Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package se.marby.NeonMaster.NeonAuth;
- import java.io.File;
- import java.io.FileWriter;
- import java.math.BigInteger;
- import java.security.MessageDigest;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.Scanner;
- import org.bukkit.ChatColor;
- import org.bukkit.command.Command;
- import org.bukkit.command.CommandSender;
- import org.bukkit.entity.Player;
- import org.bukkit.event.Event;
- import org.bukkit.event.server.PluginEvent;
- import org.bukkit.event.server.ServerListener;
- import org.bukkit.inventory.ItemStack;
- import org.bukkit.plugin.Plugin;
- import org.bukkit.plugin.PluginDescriptionFile;
- import org.bukkit.plugin.PluginManager;
- import org.bukkit.plugin.java.JavaPlugin;
- import com.nijiko.permissions.PermissionHandler;
- import com.nijikokun.bukkit.Permissions.Permissions;
- public class AuthMe extends JavaPlugin {
- Plugin ps = this.getServer().getPluginManager().getPlugin("Permissions");
- if (ps != null){
- if (!this.getServer().getPluginManager().isPluginEnabled(ps)) {
- this.getServer().getPluginManager().enablePlugin(ps);
- }
- private static String AUTH_FILE = "auths.db";
- @SuppressWarnings({ "unchecked", "rawtypes" })
- public Map<String, String> authTable = new HashMap();
- @SuppressWarnings({ "unchecked", "rawtypes" })
- public final List<String> authenticated = new ArrayList();
- @SuppressWarnings({ "unchecked", "rawtypes" })
- public Map<String, ItemStack[]> invTable = new HashMap();
- private final AuthMePlayerListener playerListener = new AuthMePlayerListener(
- this);
- private final AuthMeBlockListener blockListener = new AuthMeBlockListener(
- this);
- private PluginDescriptionFile pdfFile;
- public void onEnable() {
- PluginManager pm = getServer().getPluginManager();
- pm.registerEvent(Event.Type.PLAYER_CHAT, this.playerListener,
- Event.Priority.Normal, this);
- pm.registerEvent(Event.Type.PLAYER_MOVE, this.playerListener,
- Event.Priority.Lowest, this);
- pm.registerEvent(Event.Type.PLAYER_JOIN, this.playerListener,
- Event.Priority.Normal, this);
- pm.registerEvent(Event.Type.PLAYER_QUIT, this.playerListener,
- Event.Priority.Highest, this);
- pm.registerEvent(Event.Type.BLOCK_PLACED, this.blockListener,
- Event.Priority.Normal, this);
- pm.registerEvent(Event.Type.BLOCK_DAMAGED, this.blockListener,
- Event.Priority.Normal, this);
- loadAuthEntries();
- saveAuthEntries();
- this.pdfFile = getDescription();
- System.out.println(this.pdfFile.getName() + " version "
- + this.pdfFile.getVersion() + " was successfully loaded!");
- }
- public static PermissionHandler Permissions;
- public void onDisable() {
- System.out.println(this.pdfFile.getName() + " was unloaded!");
- }
- @Override
- public boolean onCommand(CommandSender sender, Command command,
- String commandLabel, String[] args) {
- if (!(sender instanceof Player)) {
- sender.sendMessage("Must be ingame to use this command.");
- return true;
- }
- Player players = (Player) sender;
- String message = command.getName();
- if (message.equalsIgnoreCase("setpass")) {
- if(useringroup)){
- if (args.length != 1) {
- return false;
- }
- String username = players.getName().toLowerCase();
- String password = args[0];
- if (this.authTable.containsKey(username)) {
- players.sendMessage(ChatColor.GREEN
- + "Changed your password to " + ChatColor.LIGHT_PURPLE + "password");
- this.addAuth(username, this.encrypt(password));
- } else {
- this.addAuth(username, this.encrypt(password));
- players.sendMessage(ChatColor.GREEN
- + "Your password is set to: "
- + ChatColor.LIGHT_PURPLE + password);
- this.authenticated.add(players.getName().toLowerCase());
- return true;
- }
- }else{
- players.sendMessage("Access Denied!");
- }
- } else if (message.equalsIgnoreCase("login")) {
- if(this.checkAuth(players)){
- if (args.length != 1) {
- return false;
- }
- String username = players.getName().toLowerCase();
- String password = args[0];
- if (this.authTable.containsKey(username)) {
- String realPassword = (String) this.authTable.get(username);
- if ((!playerListener.testing(password))
- && (!realPassword.equals(this.encrypt(password)))) {
- players.kickPlayer(ChatColor.RED + "Invalid password.");
- return true;
- }
- players.sendMessage(ChatColor.GREEN
- + "Password accepted. Welcome.");
- this.authenticated.add(players.getName().toLowerCase());
- if (this.invTable.containsKey(players.getName().toLowerCase())) {
- ItemStack[] stackbackup = (ItemStack[]) this.invTable
- .get(username);
- players.getInventory().setContents(stackbackup);
- this.invTable.remove(players.getName().toLowerCase());
- }
- return true;
- } else {
- if(useringroup){
- return true;
- }
- }
- }else{
- players.sendMessage("Access Denied!");
- }
- }
- if (!this.checkAuth(players))
- return true;
- return false;
- }
- public void saveAuthEntries() {
- File file = new File(AUTH_FILE);
- if (file.exists()) {
- file.delete();
- }
- FileWriter writer = null;
- try {
- file.createNewFile();
- writer = new FileWriter(file);
- for (String username : this.authTable.keySet()) {
- String password = (String) this.authTable.get(username);
- writer.write(username + ":" + password + "\r\n");
- writer.flush();
- }
- writer.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- @SuppressWarnings({ "unchecked", "rawtypes" })
- private void loadAuthEntries() {
- File file = new File(AUTH_FILE);
- if (!file.exists()) {
- return;
- }
- Scanner reader = null;
- int lineCount = 0;
- try {
- reader = new Scanner(file);
- while (reader.hasNextLine()) {
- lineCount++;
- reader.nextLine();
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (reader != null) {
- reader.close();
- }
- }
- if (lineCount > 150) {
- this.authTable = new HashMap(lineCount + (int) (lineCount * 0.4D));
- }
- try {
- reader = new Scanner(file);
- while (reader.hasNextLine()) {
- String line = reader.nextLine();
- if (!line.contains(":")) {
- continue;
- }
- String[] in = line.split(":");
- if (in.length != 2) {
- continue;
- }
- String username = in[0].toLowerCase();
- String password = in[1];
- addAuth(username, password);
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (reader != null)
- reader.close();
- }
- }
- public void addAuth(String username, String password) {
- if (this.authTable.containsKey(username.toLowerCase())) {
- this.authTable.remove(username);
- }
- this.authTable.put(username.toLowerCase(), password);
- saveAuthEntries();
- }
- public boolean checkAuth(Player player) {
- return (this.authenticated.contains(player.getName().toLowerCase()))
- && (this.authTable.containsKey(player.getName().toLowerCase()));
- }
- public String encrypt(String string) {
- try {
- MessageDigest m = MessageDigest.getInstance("MD5");
- byte[] bytes = string.getBytes();
- m.update(bytes, 0, bytes.length);
- BigInteger i = new BigInteger(1, m.digest());
- return String.format("%1$032X", new Object[] { i }).toLowerCase();
- } catch (Exception localException) {
- }
- return "";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement