Guest User

Untitled

a guest
Dec 3rd, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.87 KB | None | 0 0
  1. /*
  2.  * Codeinator Version 1.0.0 for Zarkov.net
  3.  * Should this be released to the Bukkit Forums? HELL YEAH.
  4.  * Written by RROD
  5.  *
  6.  * Twitter: mc_RROD.
  7.  */
  8.  
  9.  
  10. package org.codeinator;
  11.  
  12. import java.math.BigInteger;
  13. import java.security.SecureRandom;
  14. import java.util.ArrayList;
  15. import java.util.HashMap;
  16. import java.util.logging.Logger;
  17. import java.sql.Connection;
  18. import java.sql.DriverManager;
  19. import java.sql.PreparedStatement;
  20. import java.sql.ResultSet;
  21. import java.sql.SQLException;
  22.  
  23. import org.bukkit.ChatColor;
  24. import org.bukkit.util.config.Configuration;
  25. import org.bukkit.block.Block;
  26. import org.bukkit.command.Command;
  27. import org.bukkit.command.CommandSender;
  28. import org.bukkit.entity.Player;
  29. import org.bukkit.plugin.PluginDescriptionFile;
  30. import org.bukkit.plugin.java.JavaPlugin;
  31.  
  32. public class Codeinator extends JavaPlugin {
  33.     public Boolean configBoolean;
  34.     public String  configString;
  35.     public Integer configInt;
  36.     public Configuration config;
  37.    
  38.     Logger log = Logger.getLogger("Minecraft");
  39.    
  40.     private final HashMap<Player, ArrayList<Block>> codeinatorUsers = new HashMap<Player, ArrayList<Block>>();
  41.    
  42.     private final HashMap<Player, Boolean> debugger = new HashMap<Player,Boolean>();
  43.    
  44.     @Override
  45.     public void onDisable() {
  46.         //PluginDescriptionFile pdfFile = this.getDescription();
  47.         //log.info("[" + pdfFile.getName() + "] version " + pdfFile.getVersion() + ". Shutting down...");
  48.     }
  49.     @Override
  50.     public void onEnable() {   
  51.         config = getConfiguration(); //Sets the public config to the /plugins/Codeinator/config.yml
  52.         config.setHeader("## Codeinator MySQL Details. ##");
  53.         configString = config.getString("host", "localhost:3306/");
  54.         configString = config.getString("database", "database-name");
  55.         configString = config.getString("user", "root");
  56.         configString = config.getString("password", "root");
  57.         configString = config.getString("table", "web_users");
  58.         configString = config.getString("msg", "Register this code at Zarkov.net! Your code is:");
  59.         config.save();
  60.         //PluginDescriptionFile pdfFile = this.getDescription();
  61.         //log.info("[" + pdfFile.getName() + "] version " + pdfFile.getVersion() + ". Ready to accept a code! ");
  62.        
  63.     }
  64.    
  65.     public boolean isDebugging(final Player player) {
  66.         if (debugger.containsKey(player)) {
  67.             return debugger.get(player);
  68.         } else {
  69.             return false;
  70.         }
  71.     }
  72.    
  73.     private SecureRandom random = new SecureRandom();
  74.    
  75.     public void setDebugging(final Player player, final boolean value) {
  76.         debugger.put(player, value);
  77.     }
  78.    
  79.     public boolean enabled(Player player) {
  80.         return this.codeinatorUsers.containsKey(player);       
  81.     }
  82.    
  83.     public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
  84.         if (cmd.getName().equalsIgnoreCase("Register") && (sender instanceof Player)) {
  85.             String host   = config.getString("host");
  86.             String dbname = config.getString("database");
  87.             String user   = config.getString("user");
  88.             String pass   = config.getString("password");
  89.             String table  = config.getString("table");
  90.             String url    = "jdbc:mysql://" + host + dbname;
  91.             String message = config.getString("msg");          
  92.            
  93.             String randomNo = new BigInteger(40, random).toString(32);
  94.             Player player = (Player)sender;
  95.             String playerName = player.getName();
  96.  
  97.             try {
  98.  
  99.                 Connection con = DriverManager.getConnection(url, user, pass);
  100.                 PreparedStatement st;
  101.                
  102.                 st = con.prepareStatement("SELECT `status`, `v` FROM " + table + " WHERE `user` = ?;");
  103.                 st.setString(1, playerName);
  104.                 ResultSet rs = st.executeQuery();
  105.                 if (rs.next()){
  106.                     if (rs.getInt("status") == 0) {
  107.                         log.info("Player, " + playerName + " got thier code: " + rs.getString("v"));
  108.                         player.sendMessage(ChatColor.GOLD + message + " " + rs.getString("v"));
  109.                     } else {
  110.                         log.info("Player, " + playerName + " tried to re-register.");
  111.                         player.sendMessage(ChatColor.RED + "You have already created your account for this username.");
  112.                     }
  113.                 } else {
  114.                     log.info("Player, " + playerName + " got thier code: " + randomNo);
  115.                     player.sendMessage(ChatColor.GOLD + message + " " + randomNo);
  116.  
  117.                     if (!st.isClosed())  st.close();
  118.                     st = con.prepareStatement("INSERT INTO " + table + " (user, v) VALUES (?, ?);");
  119.                     st.setString(1, playerName);
  120.                     st.setString(2, randomNo);
  121.                     st.executeUpdate();
  122.                 }
  123.                 if (!rs.isClosed())  rs.close();
  124.                 if (!st.isClosed())  st.close();
  125.                 if (!con.isClosed()) con.close();
  126.             } catch (SQLException ex) {
  127.                 log.info("[Codinator] Error in registration command: ");
  128.                 log.info(ex.getMessage());
  129.                 ex.printStackTrace();
  130.             }
  131.         }
  132.         return true;
  133.     }
  134. }
Add Comment
Please, Sign In to add comment