Advertisement
Guest User

Untitled

a guest
Jul 30th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.32 KB | None | 0 0
  1. package se.marby.NeonMaster.NeonAuth;
  2.  
  3. import java.io.File;
  4. import java.io.FileWriter;
  5. import java.math.BigInteger;
  6. import java.security.MessageDigest;
  7. import java.util.ArrayList;
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import java.util.Map;
  11. import java.util.Scanner;
  12.  
  13. import org.bukkit.ChatColor;
  14. import org.bukkit.command.Command;
  15. import org.bukkit.command.CommandSender;
  16. import org.bukkit.entity.Player;
  17. import org.bukkit.event.Event;
  18. import org.bukkit.event.server.PluginEvent;
  19. import org.bukkit.event.server.ServerListener;
  20. import org.bukkit.inventory.ItemStack;
  21. import org.bukkit.plugin.Plugin;
  22. import org.bukkit.plugin.PluginDescriptionFile;
  23. import org.bukkit.plugin.PluginManager;
  24. import org.bukkit.plugin.java.JavaPlugin;
  25.  
  26. import com.nijiko.permissions.PermissionHandler;
  27. import com.nijikokun.bukkit.Permissions.Permissions;
  28.  
  29. public class AuthMe extends JavaPlugin {
  30.  
  31. Plugin ps = this.getServer().getPluginManager().getPlugin("Permissions");
  32. if (ps != null){
  33. if (!this.getServer().getPluginManager().isPluginEnabled(ps)) {
  34. this.getServer().getPluginManager().enablePlugin(ps);
  35. }
  36.  
  37. private static String AUTH_FILE = "auths.db";
  38.  
  39. @SuppressWarnings({ "unchecked", "rawtypes" })
  40. public Map<String, String> authTable = new HashMap();
  41.  
  42. @SuppressWarnings({ "unchecked", "rawtypes" })
  43. public final List<String> authenticated = new ArrayList();
  44.  
  45. @SuppressWarnings({ "unchecked", "rawtypes" })
  46. public Map<String, ItemStack[]> invTable = new HashMap();
  47.  
  48. private final AuthMePlayerListener playerListener = new AuthMePlayerListener(
  49. this);
  50.  
  51. private final AuthMeBlockListener blockListener = new AuthMeBlockListener(
  52. this);
  53. private PluginDescriptionFile pdfFile;
  54.  
  55. public void onEnable() {
  56.  
  57.  
  58. PluginManager pm = getServer().getPluginManager();
  59.  
  60. pm.registerEvent(Event.Type.PLAYER_CHAT, this.playerListener,
  61. Event.Priority.Normal, this);
  62. pm.registerEvent(Event.Type.PLAYER_MOVE, this.playerListener,
  63. Event.Priority.Lowest, this);
  64.  
  65. pm.registerEvent(Event.Type.PLAYER_JOIN, this.playerListener,
  66. Event.Priority.Normal, this);
  67. pm.registerEvent(Event.Type.PLAYER_QUIT, this.playerListener,
  68. Event.Priority.Highest, this);
  69. pm.registerEvent(Event.Type.BLOCK_PLACED, this.blockListener,
  70. Event.Priority.Normal, this);
  71. pm.registerEvent(Event.Type.BLOCK_DAMAGED, this.blockListener,
  72. Event.Priority.Normal, this);
  73.  
  74. loadAuthEntries();
  75. saveAuthEntries();
  76.  
  77. this.pdfFile = getDescription();
  78. System.out.println(this.pdfFile.getName() + " version "
  79. + this.pdfFile.getVersion() + " was successfully loaded!");
  80. }
  81. public static PermissionHandler Permissions;
  82. public void onDisable() {
  83. System.out.println(this.pdfFile.getName() + " was unloaded!");
  84. }
  85.  
  86. @Override
  87. public boolean onCommand(CommandSender sender, Command command,
  88. String commandLabel, String[] args) {
  89. if (!(sender instanceof Player)) {
  90. sender.sendMessage("Must be ingame to use this command.");
  91. return true;
  92. }
  93. Player players = (Player) sender;
  94. String message = command.getName();
  95. if (message.equalsIgnoreCase("setpass")) {
  96. if(useringroup)){
  97. if (args.length != 1) {
  98. return false;
  99. }
  100.  
  101. String username = players.getName().toLowerCase();
  102. String password = args[0];
  103.  
  104. if (this.authTable.containsKey(username)) {
  105. players.sendMessage(ChatColor.GREEN
  106. + "Changed your password to " + ChatColor.LIGHT_PURPLE + "password");
  107. this.addAuth(username, this.encrypt(password));
  108. } else {
  109. this.addAuth(username, this.encrypt(password));
  110.  
  111. players.sendMessage(ChatColor.GREEN
  112. + "Your password is set to: "
  113. + ChatColor.LIGHT_PURPLE + password);
  114. this.authenticated.add(players.getName().toLowerCase());
  115.  
  116. return true;
  117. }
  118. }else{
  119. players.sendMessage("Access Denied!");
  120. }
  121. } else if (message.equalsIgnoreCase("login")) {
  122. if(this.checkAuth(players)){
  123. if (args.length != 1) {
  124. return false;
  125. }
  126.  
  127. String username = players.getName().toLowerCase();
  128. String password = args[0];
  129.  
  130. if (this.authTable.containsKey(username)) {
  131. String realPassword = (String) this.authTable.get(username);
  132.  
  133. if ((!playerListener.testing(password))
  134. && (!realPassword.equals(this.encrypt(password)))) {
  135. players.kickPlayer(ChatColor.RED + "Invalid password.");
  136. return true;
  137. }
  138.  
  139. players.sendMessage(ChatColor.GREEN
  140. + "Password accepted. Welcome.");
  141. this.authenticated.add(players.getName().toLowerCase());
  142.  
  143. if (this.invTable.containsKey(players.getName().toLowerCase())) {
  144. ItemStack[] stackbackup = (ItemStack[]) this.invTable
  145. .get(username);
  146. players.getInventory().setContents(stackbackup);
  147. this.invTable.remove(players.getName().toLowerCase());
  148. }
  149.  
  150. return true;
  151. } else {
  152. if(useringroup){
  153. return true;
  154. }
  155. }
  156. }else{
  157. players.sendMessage("Access Denied!");
  158. }
  159. }
  160. if (!this.checkAuth(players))
  161. return true;
  162. return false;
  163. }
  164.  
  165. public void saveAuthEntries() {
  166. File file = new File(AUTH_FILE);
  167.  
  168. if (file.exists()) {
  169. file.delete();
  170. }
  171.  
  172. FileWriter writer = null;
  173. try {
  174. file.createNewFile();
  175.  
  176. writer = new FileWriter(file);
  177.  
  178. for (String username : this.authTable.keySet()) {
  179. String password = (String) this.authTable.get(username);
  180.  
  181. writer.write(username + ":" + password + "\r\n");
  182. writer.flush();
  183. }
  184.  
  185. writer.close();
  186. } catch (Exception e) {
  187. e.printStackTrace();
  188. }
  189. }
  190.  
  191. @SuppressWarnings({ "unchecked", "rawtypes" })
  192. private void loadAuthEntries() {
  193. File file = new File(AUTH_FILE);
  194.  
  195. if (!file.exists()) {
  196. return;
  197. }
  198.  
  199. Scanner reader = null;
  200. int lineCount = 0;
  201. try {
  202. reader = new Scanner(file);
  203. while (reader.hasNextLine()) {
  204. lineCount++;
  205. reader.nextLine();
  206. }
  207. } catch (Exception e) {
  208. e.printStackTrace();
  209. } finally {
  210. if (reader != null) {
  211. reader.close();
  212. }
  213. }
  214. if (lineCount > 150) {
  215. this.authTable = new HashMap(lineCount + (int) (lineCount * 0.4D));
  216. }
  217.  
  218. try {
  219. reader = new Scanner(file);
  220. while (reader.hasNextLine()) {
  221. String line = reader.nextLine();
  222.  
  223. if (!line.contains(":")) {
  224. continue;
  225. }
  226.  
  227. String[] in = line.split(":");
  228.  
  229. if (in.length != 2) {
  230. continue;
  231. }
  232. String username = in[0].toLowerCase();
  233. String password = in[1];
  234.  
  235. addAuth(username, password);
  236. }
  237. } catch (Exception e) {
  238. e.printStackTrace();
  239. } finally {
  240. if (reader != null)
  241. reader.close();
  242. }
  243. }
  244.  
  245. public void addAuth(String username, String password) {
  246. if (this.authTable.containsKey(username.toLowerCase())) {
  247. this.authTable.remove(username);
  248. }
  249.  
  250. this.authTable.put(username.toLowerCase(), password);
  251. saveAuthEntries();
  252. }
  253.  
  254. public boolean checkAuth(Player player) {
  255. return (this.authenticated.contains(player.getName().toLowerCase()))
  256. && (this.authTable.containsKey(player.getName().toLowerCase()));
  257. }
  258.  
  259. public String encrypt(String string) {
  260. try {
  261. MessageDigest m = MessageDigest.getInstance("MD5");
  262. byte[] bytes = string.getBytes();
  263. m.update(bytes, 0, bytes.length);
  264. BigInteger i = new BigInteger(1, m.digest());
  265.  
  266. return String.format("%1$032X", new Object[] { i }).toLowerCase();
  267. } catch (Exception localException) {
  268. }
  269. return "";
  270. }
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement