Advertisement
Rene98

Main Class

Mar 2nd, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.78 KB | None | 0 0
  1. package com.jartexnetwork.skyblock.gui;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. import java.util.ArrayList;
  8.  
  9. import org.bukkit.Bukkit;
  10. import org.bukkit.ChatColor;
  11. import org.bukkit.Material;
  12. import org.bukkit.command.Command;
  13. import org.bukkit.command.CommandSender;
  14. import org.bukkit.entity.Player;
  15. import org.bukkit.event.Listener;
  16. import org.bukkit.event.inventory.InventoryClickEvent;
  17. import org.bukkit.inventory.Inventory;
  18. import org.bukkit.inventory.ItemStack;
  19. import org.bukkit.inventory.meta.ItemMeta;
  20. import org.bukkit.inventory.meta.SkullMeta;
  21. import org.bukkit.plugin.java.JavaPlugin;
  22.  
  23. public class Main extends JavaPlugin implements Listener{
  24.  
  25.     static Connection connection;
  26.     static Statement statement;
  27.     public void onEnable() {
  28.         this.getConfig().options().copyDefaults();
  29.         saveConfig();
  30.         Bukkit.getServer().getPluginManager().registerEvents(this, this);
  31.  
  32.         try { //We use a try catch to avoid errors, hopefully we don't get any.
  33.             Class.forName("com.mysql.jdbc.Driver"); //this accesses Driver in jdbc.
  34.         } catch (ClassNotFoundException e) {
  35.             e.printStackTrace();
  36.             System.err.println("jdbc driver unavailable!");
  37.             return;
  38.         }
  39.         /*final String url = this.getConfig().getString("Url");
  40.         final String username = this.getConfig().getString("Username");
  41.         final String password = this.getConfig().getString("Password");*/
  42.         try { //Another try catch to get any SQL errors (for example connections errors)
  43.             connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/spawnerrankupsystem","root","JartexRene98");
  44.             //with the method getConnection() from DriverManager, we're trying to set
  45.             //the connection's url, username, password to the variables we made earlier and
  46.             //trying to get a connection at the same time. JDBC allows us to do this.
  47.         } catch (SQLException e) { //catching errors)
  48.             e.printStackTrace(); //prints out SQLException errors to the console (if any)
  49.             System.err.println("Failed connection with MySQL");
  50.             Bukkit.getPluginManager().disablePlugin(this);
  51.         }
  52.     }
  53.  
  54.  
  55.     public void onDisable() {      
  56.         try { //using a try catch to catch connection errors (like wrong sql password...)
  57.             if(connection!=null && !connection.isClosed()){ //checking if connection isn't null to
  58.                 //avoid recieving a nullpointer
  59.                 connection.close(); //closing the connection field variable.
  60.             }
  61.         }catch(Exception e){
  62.             e.printStackTrace();
  63.  
  64.         }
  65.         Bukkit.getServer().getLogger().info("SpawnerRankupSystem is Disabled!");
  66.     }
  67.  
  68.  
  69.     public void OnCommand(CommandSender sender, Command cmd, String label, String[] args) {
  70.         String p = sender.getName().toLowerCase();
  71.  
  72.         int stage = 0;
  73.         int heads = 0;
  74.         boolean money = false;
  75.         if(cmd.getName().equalsIgnoreCase("Spawner")) {
  76.  
  77.  
  78.             try {
  79.                 statement = connection.createStatement();
  80.             } catch (SQLException ignored) {}
  81.  
  82.             String sql = "SELECT spawnerrankupsystem "
  83.                     + "FROM players"
  84.                     + "WHERE  name LIKE '%' + name + '%' ";
  85.             String sql2 = "INSERT into players"
  86.                     + "playername, stage, heads, money"
  87.                     + "VALUES(" + p + ", " + stage + ", " + heads + ", " + money;
  88.  
  89.             try {
  90.                 statement.executeQuery(sql);
  91.             } catch (SQLException ignored) {
  92.                 try {
  93.                     statement.executeQuery(sql2);
  94.                 } catch (SQLException e) {
  95.                     e.printStackTrace();
  96.                 }
  97.             }
  98.  
  99.            
  100.             ItemStack skull = new ItemStack(Material.SKULL_ITEM, 1, (short) 3);
  101.             ItemStack skull2 = (ItemStack)skull.getItemMeta();
  102.             ((SkullMeta) skull2).setOwner("MHF_ArrowUp");
  103.             ((ItemMeta) skull2).setDisplayName(ChatColor.GOLD + "RANKUP");
  104.             Inventory inv = Bukkit.createInventory(null, 8, ChatColor.DARK_AQUA + "Spawner GUI");
  105.             inv.setItem(0, skull2);
  106.             inv.setItem(4, new ItemStack(Material.MOB_SPAWNER));
  107.  
  108.  
  109.             ArrayList<String> lore = new ArrayList<String>();
  110.             //Formula heads needed: 500+55x^2
  111.             double percentage = 6;
  112.             int amount = (int) Math.floor(percentage);
  113.             lore.add("Status:" + amount);
  114.             ItemStack ladder = new ItemStack(Material.LADDER, 1 , (short) 0);
  115.             ItemMeta Ladder = ladder.getItemMeta();
  116.             inv.setItem(8, new ItemStack(Material.LADDER));
  117.             Player player = Bukkit.getPlayer(p);
  118.             Ladder.setLore(lore);
  119.             ladder.setItemMeta((ItemMeta) ladder);
  120.             player.openInventory(inv);
  121.         }
  122.     }
  123.  
  124.  
  125.     public void onInventoryClickEvent(InventoryClickEvent e) {
  126.         if(e.getInventory().getName().equalsIgnoreCase("Spawner"))
  127.             return;
  128.         Player p = (Player) e.getWhoClicked();
  129.         e.setCancelled(true);
  130.  
  131.         if(e.getCurrentItem()==null || e.getCurrentItem().getType()==Material.AIR||!e.getCurrentItem().hasItemMeta()){
  132.             p.closeInventory();
  133.             return;
  134.         }
  135.  
  136.         switch(e.getCurrentItem().getType()) {
  137.         case SKULL_ITEM:
  138.             Inventory rankUp = Bukkit.createInventory(null, 53, ChatColor.BLUE + "RANKUP");
  139.             ItemStack dye = new ItemStack(Material.INK_SACK, 1, (short) 10);
  140.             rankUp.setItem(47, new ItemStack(dye));
  141.             rankUp.setItem(51, new ItemStack(Material.GOLD_BLOCK));
  142.             p.closeInventory();
  143.             p.openInventory(rankUp);
  144.             break;
  145.         case MOB_SPAWNER:
  146.             Inventory spawners = Bukkit.createInventory(null, 17, ChatColor.DARK_GREEN + "Spawners");
  147.             String[] Aspawners = {"Pig","Cow","Sheep","Chicken","Mooshroom",
  148.                     "Skeleton","Zombie","Creeper","Spider","Blaze","Enderman",
  149.                     "Slime","Pigman","Cavespider","Iron Golem"};
  150.             for(int i=0; i <= Aspawners.length; i++) {
  151.                 if(p.hasPermission("silkspawners.place." + Aspawners[i])){
  152.                     ItemStack spawner = new ItemStack(Material.MOB_SPAWNER);
  153.                     spawner.getItemMeta().setDisplayName(ChatColor.YELLOW + Aspawners[i] + "Spawner");
  154.                     spawners.setItem(i, spawner);
  155.                 }
  156.             }
  157.             p.closeInventory();
  158.             p.openInventory(spawners);
  159.             break;
  160.         case LADDER:
  161.             p.closeInventory();
  162.             break;
  163.         default:
  164.             p.closeInventory();
  165.             break;
  166.  
  167.         }
  168.     }
  169.  
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement