Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 18.53 KB | None | 0 0
  1. package me.zachbears27;
  2. import java.io.Console;
  3. import java.sql.Connection;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import org.bukkit.Bukkit;
  7. import org.bukkit.Location;
  8. import org.bukkit.Server;
  9. import org.bukkit.command.Command;
  10. import org.bukkit.command.CommandSender;
  11. import org.bukkit.command.ConsoleCommandSender;
  12. import org.bukkit.conversations.ConversationAbandonedListener;
  13. import org.bukkit.entity.Player;
  14. import org.bukkit.event.EventHandler;
  15. import org.bukkit.event.Listener;
  16. import org.bukkit.event.entity.PlayerDeathEvent;
  17. import org.bukkit.inventory.ItemStack;
  18. import org.bukkit.plugin.java.JavaPlugin;
  19. import java.sql.Statement;
  20. import java.text.ParseException;
  21. import java.text.SimpleDateFormat;
  22. import java.util.Date;
  23. import java.util.HashMap;
  24. import java.util.UUID;
  25. import java.util.concurrent.TimeUnit;
  26.  
  27. import me.zachbears27.MySQL;
  28. import net.md_5.bungee.api.ChatColor;
  29.  
  30. //This Plugin Is Made For MumboCraft & It's Players & Staff.. Any misuse or re-distribution of this plugin will call for said plugin to be removed
  31. //Plugin by A_Brave_Panda & Jamdoggy
  32.  
  33. public class JumbleHill extends JavaPlugin implements Listener {
  34.    
  35.       HashMap<String, ItemStack[]> inventoryContents = new HashMap<String, ItemStack[]>();
  36.       HashMap<String, ItemStack[]> inventoryArmorContents = new HashMap<String, ItemStack[]>();
  37.       Server server = Bukkit.getServer();
  38.       ConsoleCommandSender console = server.getConsoleSender();
  39.  
  40. MySQL MySQLC = null;
  41. Connection c = null;
  42.  
  43. String host = getConfig().getString("Hostname");
  44. String port = getConfig().getString("Port");
  45. String db = getConfig().getString("Database");
  46. String username = getConfig().getString("Username");
  47. String password = getConfig().getString("Password");
  48. String table = getConfig().getString("Table");
  49. String intro = ChatColor.GREEN + "" + ChatColor.BOLD + "Jumble Hill " + ChatColor.GRAY + "" + ChatColor.BOLD + ">> ";
  50. String itemtable = "items";
  51.            
  52.  
  53.  @Override
  54.  public void onDisable() {
  55.  
  56.  }
  57.  
  58.  @Override
  59.  public void onEnable() {
  60.       getServer().getPluginManager().registerEvents(this, this);
  61.       registerConfig();
  62.       MySQL MySQLC = new MySQL(host, port, db, username, password);
  63.   try {
  64.     c = MySQLC.openConnection();
  65. } catch (ClassNotFoundException e) {
  66.     e.printStackTrace();
  67. } catch (SQLException e) {
  68.  
  69.     e.printStackTrace();
  70. }
  71.  
  72. System.out.print("[JumbleHill] Plugin Has Been Loaded!");
  73.  
  74. }
  75.  
  76.  public void registerConfig() {
  77.     saveDefaultConfig();
  78.    
  79.    
  80.    
  81.        
  82.     }
  83.  
  84.  
  85.  java.util.Date dt = new java.util.Date();
  86.  
  87.  java.text.SimpleDateFormat sdf =
  88.       new java.text.SimpleDateFormat("MM/dd/yy HH:mm:ss");
  89.     Date now = new Date();
  90.      SimpleDateFormat format = new SimpleDateFormat("MM/dd/yy HH:mm:ss");
  91.  
  92.  
  93.   public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
  94.     if (sender instanceof Player) {
  95.         Player p = (Player)sender;
  96.  if(cmd.getLabel().equalsIgnoreCase("jumblehill")) {
  97.       if(args.length == 0) {
  98.           p.sendMessage(intro + ChatColor.RED + " Incorrect Arguments! Please try: '/jumblehill help'");
  99.       } else if(args[0].equals("help")) {
  100.           p.sendMessage(intro + ChatColor.AQUA + " /jumblehill search");
  101.           p.sendMessage(intro + ChatColor.AQUA + " /jumblehill stats");
  102.           p.sendMessage(intro + ChatColor.AQUA + " /jumblehill set");
  103.           p.sendMessage(intro + ChatColor.AQUA + " /jumblehill add {item name} {item amount} {item price in diamonds}");
  104.           p.sendMessage(intro + ChatColor.AQUA + " /jumblehill remove {item name}");
  105.           p.sendMessage(intro + ChatColor.AQUA + " /jumblehill list");
  106.       } else if(args[0].equals("search")) {
  107.          
  108.             /* new AnvilGUI(this, p, new AnvilGUI.AnvilClickHandler() {
  109.                  @Override
  110.                  public boolean onClick(AnvilGUI menu, String text){
  111.                      menu.getPlayer().sendMessage(intro + ChatColor.GOLD + "You have chosen: " + ChatColor.AQUA + text); */
  112.   String itemselect = "SELECT * FROM " + itemtable + " WHERE itemname = '" + args[1] +"' ORDER BY shop_id;";
  113.   String shopselect = "";
  114.  
  115. try {
  116.      
  117.      Statement statement = c.createStatement();
  118.      ResultSet res2 = null;
  119.      Statement statement2 = c.createStatement();
  120.      ResultSet res = statement2.executeQuery(itemselect);
  121.  
  122.  
  123.   int count = 0;
  124.   int shopcount = 0;
  125.   int lastID = 0;
  126.  
  127.   p.sendMessage("Searching for: " + args[1]);
  128.   while(res.next()) {
  129.     count++;
  130.  
  131.     // New Shop ID - show the name
  132.     if (lastID != res.getInt("shop_id")) {
  133.       lastID = res.getInt("shop_id");
  134.       shopcount++;
  135.      
  136.       // Grab shop details
  137.       shopselect = "SELECT * FROM " + table + " WHERE id = '" + res.getInt("shop_id") +"';";
  138.       res2 = statement.executeQuery(shopselect);
  139.      if(res2.next()) {
  140.       p.sendMessage(intro + ChatColor.GOLD + "Shop: " + ChatColor.GREEN + ChatColor.BOLD + res2.getString("shopname") + ChatColor.GOLD + " at" + ChatColor.GREEN + ChatColor.BOLD + " [" + res2.getInt("x") + ", " + res2.getInt("y") + ", " + res2.getInt("z") + "]" + ChatColor.GOLD + " owned by " + ChatColor.GREEN + ChatColor.BOLD + res2.getString("owner"));
  141.    
  142.     } else {
  143.         p.sendMessage(shopselect);
  144.         p.sendMessage("Invalid Shop");
  145.     }
  146.   }
  147.     p.sendMessage("* " + res.getInt("itemamount") + " " + res.getString("itemname") + " = " + res.getInt("itemprice") + " diamonds.");
  148.   }
  149.   //if it cant find a shop with it
  150.   if (count == 0) {
  151.     p.sendMessage("Sorry, no shops in Jumble Hill sell that item.");
  152.   } else {
  153.     p.sendMessage(intro + ChatColor.GREEN + "" + ChatColor.BOLD + count + ChatColor.GOLD + " Trades In Jumble Hill Have Been Found.");
  154.    p.sendMessage(intro + ChatColor.GREEN + "" + ChatColor.BOLD + shopcount + ChatColor.GOLD + " Shops Are Selling That Item.");
  155.  
  156.     }
  157. }
  158.              catch (SQLException e) {
  159.             // TODO Auto-generated catch block
  160.             e.printStackTrace();
  161.         }
  162.  
  163.                     /* return true;
  164.  
  165.                  
  166.                      
  167.                  }
  168.              }).setInputName("Enter An Item Name").open();  */
  169.       } else if(args[0].equals("set")) {
  170.  
  171.              Location l = p.getLocation();
  172.              double x = l.getX();
  173.              double y = l.getY();
  174.              double z = l.getZ();
  175.          
  176.              new AnvilGUI(this, p, new AnvilGUI.AnvilClickHandler() {
  177.                  @Override
  178.                  public boolean onClick(AnvilGUI menu, String text){
  179.                      String cleantext = text.replace("'", "").replace("Shop Name", "");
  180.                      String ShopName = "INSERT INTO "+ table + " (id, shopname, owner, x, y, z) VALUES (NULL, '" + cleantext + "', '" + p.getUniqueId() + "', '" + x + "', '" + y + "', '" + z + "');";
  181.                      String select = "SELECT * FROM " + table + " WHERE shopname = '" + cleantext +"';";
  182.                      try {
  183.                     Statement statement = c.createStatement();
  184.                     ResultSet res = statement.executeQuery(select);
  185.                 //If shop exists, update location
  186.                     if(res.next()) {
  187.                         //shop exists
  188.                         //if owner of shop equals player making shop then do it
  189.                         if(menu.getPlayer().getName().equalsIgnoreCase(res.getString("owner"))) {
  190.                             menu.getPlayer().sendMessage(intro + ChatColor.GOLD + "Your shop is called: " + ChatColor.AQUA + cleantext);
  191.                              menu.getPlayer().sendMessage(intro + ChatColor.GOLD + "Your shop is now located at: " + ChatColor.AQUA + (int)x + ", " + (int)y + ", " + (int)z);
  192.                              menu.getPlayer().sendMessage(intro + ChatColor.GOLD + "Remember, you can always use this command again to re-update your shop location!");
  193.                         menu.getPlayer().sendMessage(intro + ChatColor.GREEN + "Shop Location Updated!");
  194.                         String UpdateShopName = "UPDATE " + table + " SET shopname='" + cleantext + "', owner='" + p.getUniqueId() + "', x='" + x + "', y='" + y + "', z='" + z + "' WHERE id='" + res.getInt("id") + "';";
  195.                         statement.executeUpdate(UpdateShopName);
  196.                         } else {
  197.                             menu.getPlayer().sendMessage(intro + ChatColor.RED + "You must own " + cleantext + " to remove it!");
  198.                         }
  199.                         } else {
  200.                             //shop doesnt
  201.                              menu.getPlayer().sendMessage(intro + ChatColor.GOLD + "Your shop is called: " + ChatColor.AQUA + cleantext);
  202.                              menu.getPlayer().sendMessage(intro + ChatColor.GOLD + "Your shop is located at: " + ChatColor.AQUA + x + " " + y + " " + z);
  203.                              menu.getPlayer().sendMessage(intro + ChatColor.GOLD + "Remember, you can always use this command again to re-update your shop location!");
  204.                             statement.executeUpdate(ShopName);
  205.                             menu.getPlayer().sendMessage(intro + ChatColor.GREEN + "Shop Location Created!");
  206.                         }
  207.                 } catch (SQLException e2) {                
  208.                     e2.printStackTrace();
  209.                 }
  210.  
  211.                      return true;
  212.  
  213.                  }
  214.              }).setInputName("Shop Name").open();
  215.                  
  216.                
  217.       } else if(args[0].equals("add")) {
  218.           if(!(args.length < 4)) {
  219.  
  220.              new AnvilGUI(this, p, new AnvilGUI.AnvilClickHandler() {
  221.                  @Override
  222.                  public boolean onClick(AnvilGUI menu, String text){
  223.                      String cleantext = text.replace("'", "").replace("Shop Name", "");
  224.                      String nameselect = "SELECT id,owner FROM " + table + " WHERE shopname = '" + cleantext +"';";    
  225.                      
  226.                    
  227.                    
  228.                     try {
  229.                         Statement statement = c.createStatement();
  230.                         ResultSet res = statement.executeQuery(nameselect);
  231.                         if (res.next()) {
  232.                           if(p.getName().equals(res.getString("owner"))) {
  233.                               String itemselect = "SELECT * FROM " + itemtable + " WHERE shop_id = '" + res.getInt("id") +"';";
  234.                               Statement statement2 = c.createStatement();
  235.                               ResultSet res2 = statement2.executeQuery(itemselect);
  236.                               if(res2.next()) {
  237.                                 if(res2.getString("itemamount").equals(args[2]) && res2.getString("itemname").equals(args[1])) {
  238.                                     // This item and amount are already in the database, just update the price
  239.                                     String itemupdate = "UPDATE " + itemtable + " SET itemprice='" + args[3] + "' WHERE shop_id = '" + res.getInt("id") +"';";
  240.                                     p.sendMessage(itemupdate);
  241.                                     Statement statement3 = c.createStatement();
  242.                                     statement3.executeUpdate(itemupdate);
  243.                                     p.sendMessage(intro + ChatColor.GOLD + "Price Updated: " + ChatColor.GREEN + ChatColor.BOLD + args[2] + " " + args[1] + ChatColor.GOLD + " Now Costs " + ChatColor.GREEN + ChatColor.BOLD + args[3] + ChatColor.GOLD + " Diamond(s).");
  244.                                 } else {
  245.                                     // Add a new price for this shop
  246.                                     String iteminsert = "INSERT INTO "+ itemtable + " (id, shop_id, itemname, itemprice, itemamount) VALUES (NULL, '" + res.getInt("id") + "', '" + args[1] + "', '" + args[3] + "', '" + args[2] + "');";
  247.                                     Statement statement4 = c.createStatement();
  248.                                     statement4.executeUpdate(iteminsert);
  249.                                     p.sendMessage(intro + ChatColor.GOLD + "Price Added: " + ChatColor.GREEN + ChatColor.BOLD + args[2] + " " + args[1] + ChatColor.GOLD + " Costs "+ ChatColor.GREEN + ChatColor.BOLD + args[3] + ChatColor.GOLD + " Diamond(s).");
  250.                                 }
  251.                               } else {
  252.                                   // There are no prices in the database for this shop, so let's add the first one!
  253.                                   String iteminsert2 = "INSERT INTO "+ itemtable + " (id, shop_id, itemname, itemprice, itemamount) VALUES (NULL, '" + res.getInt("id") + "', '" + args[1] + "', '" + args[3] + "', '" + args[2] + "');";
  254.                                   Statement statement5 = c.createStatement();
  255.                                   statement5.executeUpdate(iteminsert2);
  256.                                   p.sendMessage(intro + ChatColor.GOLD + "Price Added: " + ChatColor.GREEN + ChatColor.BOLD + args[2] + " " + args[1] + ChatColor.GOLD + " Costs "+ ChatColor.GREEN + ChatColor.BOLD + args[3] + ChatColor.GOLD + " Diamond(s).");
  257.                                   p.sendMessage(intro + ChatColor.GREEN + "Congratulations on adding your first price to your shop!");
  258.                               }
  259.                           } else {
  260.                              // Oi!  You don't own this shop, bugger off!
  261.                               p.sendMessage(intro + ChatColor.RED + "Oi! You don't own this shop, bugger off!" + ChatColor.DARK_RED + " - Jamdoggy @ 2017" );
  262.                           }
  263.                         } else {
  264.                         // Shop not found in database...?
  265.                             p.sendMessage(intro + ChatColor.RED + "That Shop Doesn't Exist!");
  266.                         }
  267.                     } catch (SQLException e) {
  268.                         // TODO Auto-generated catch block
  269.                         e.printStackTrace();
  270.                     }
  271.                    
  272.                      
  273.                      return true;
  274.  
  275.                  }
  276.              }).setInputName("Shop Name").open();
  277.           } else {
  278.               p.sendMessage(intro + ChatColor.RED + "Incorrect Arguments! Please try: '/jumblehill add {item name} {item amount} {item price in diamonds}'");
  279.           }
  280.                
  281.       } else if(args[0].equals("remove")) {
  282.       if(!(args.length == 1)) {
  283.           new AnvilGUI(this, p, new AnvilGUI.AnvilClickHandler() {
  284.                  @Override
  285.                  public boolean onClick(AnvilGUI menu, String text){
  286.                      String cleantext = text.replace("'", "").replace("Shop Name", "");
  287.                      String nameselect = "SELECT * FROM " + table + " WHERE shopname = '" + cleantext +"';";   
  288.                      String itemselect = "SELECT * FROM " + itemtable + " WHERE itemname = '" + args[1] +"';";
  289.                    
  290.                    
  291.                     try {
  292.                         Statement statement = c.createStatement();
  293.                         ResultSet res = statement.executeQuery(nameselect);
  294.                         Statement statement2 = c.createStatement();
  295.                         ResultSet res2 = statement2.executeQuery(itemselect);
  296.                         if(res.next()) {
  297.                         if(res.getString("owner").equals(p.getName())) {
  298.                             if(res2.next()) {
  299.                             if(res2.getString("itemname").equals(args[1]) ) {
  300.                                 String itemremove = "DELETE FROM " + itemtable + " WHERE itemname = '" + args[1] +"' and shop_id = '" + res.getInt("id") + "' LIMIT 1;";
  301.                                 Statement statement3 = c.createStatement();
  302.                                 statement3.executeUpdate(itemremove);
  303.                                 p.sendMessage(intro + ChatColor.GREEN + "Item Sucessfully Removed!");
  304.                             }
  305.                             } else {
  306.                                 p.sendMessage(intro + ChatColor.RED + "This Item Isn't In That Shop!");
  307.                             }
  308.                         } else {
  309.                             p.sendMessage(intro + ChatColor.RED + "Sorry, You Don't Own This Shop!");
  310.                         }
  311.                        
  312.                         } else {
  313.                             p.sendMessage(intro + ChatColor.RED + "Error! No Shop With That Name Exists!");
  314.                         }
  315.                     } catch (SQLException e) {
  316.                         // TODO Auto-generated catch block
  317.                         e.printStackTrace();
  318.                     }
  319.                    
  320.                      
  321.                
  322.                      return true;
  323.                      
  324.                      
  325.                  }
  326.           }).setInputName("Shop Name").open();
  327.       } else {
  328.           p.sendMessage(intro + ChatColor.RED + "Incorrect Arguments! Please try: '/jumblehill remove {item name}'");
  329.       }
  330.       }  else if(args[0].equals("list")) {
  331.  
  332.       new AnvilGUI(this, p, new AnvilGUI.AnvilClickHandler() {
  333.              @Override
  334.              public boolean onClick(AnvilGUI menu, String text){
  335.                  String cleantext = text.replace("'", "").replace("Shop Name", "");
  336.                  String nameselect = "SELECT * FROM " + table + " WHERE shopname = '" + cleantext +"';";   
  337.                
  338.                
  339.                
  340.                 try {
  341.                     Statement statement = c.createStatement();
  342.                     ResultSet res = statement.executeQuery(nameselect);
  343.                    
  344.                     if(res.next()) {
  345.                         int shop_id = res.getInt("id");
  346.                         String itemselect = "SELECT * FROM " + itemtable + " WHERE shop_id = '" + shop_id +"';";
  347.                         Statement statement2 = c.createStatement();
  348.                         ResultSet res2 = statement2.executeQuery(itemselect);
  349.                         p.sendMessage(intro + ChatColor.GREEN + "" + ChatColor.BOLD + res.getString("shopname") + ChatColor.GOLD + " Owned By: " + ChatColor.GREEN + ChatColor.BOLD + res.getString("owner") + ChatColor.GOLD + " Located At: [" + ChatColor.GREEN + ChatColor.BOLD + res.getString("x") + ", " + res.getString("y") + ", " + res.getString("z") + ChatColor.GOLD + "]");
  350.                         while(res2.next()) {
  351.                        
  352.                        
  353.                             p.sendMessage(intro + ChatColor.GRAY + "   - " + ChatColor.GREEN + "" + ChatColor.BOLD + "x" + res2.getString("itemamount") + " " + res2.getString("itemname") + ChatColor.GOLD + " For " + ChatColor.GREEN + ChatColor.BOLD + res2.getString("itemprice") + ChatColor.GOLD + " Diamond(s)!");
  354.                         }
  355.                     }
  356.                    
  357.                 } catch (SQLException e) {
  358.                     // TODO Auto-generated catch block
  359.                     e.printStackTrace();
  360.                 }
  361.                
  362.                  
  363.            
  364.                  return true;
  365.                  
  366.                  
  367.              }
  368.       }).setInputName("Shop Name").open();
  369.  
  370.  }  else if(args[0].equals("stats")) {
  371.      
  372.         String shoplist = "SELECT * FROM " + table + ";";  
  373.         if(p.hasPermission("jumblehill.stats")) {
  374.           try {
  375.               Statement statement = c.createStatement();
  376.               ResultSet res = statement.executeQuery(shoplist);
  377.        
  378.               // Go through list of shops    
  379.               while(res.next()) {
  380.                   int shop_id = res.getInt("id");
  381.        
  382.                   // Output this Shop's Info        
  383.                   p.sendMessage(ChatColor.GRAY + "[----------------------]");
  384.                   p.sendMessage(intro + ChatColor.GREEN + ChatColor.BOLD + res.getString("shopname") + ChatColor.GOLD + " Owned By: " + ChatColor.GREEN + ChatColor.BOLD + res.getString("owner") + ChatColor.GOLD + " Located At: [" + ChatColor.GREEN + ChatColor.BOLD + res.getString("x") + ", " + res.getString("y") + ", " + res.getString("z") + ChatColor.GOLD + "]");
  385.        
  386.                   // Get the list of items in this shop
  387.                   String itemselect = "SELECT * FROM " + itemtable + " WHERE shop_id = '" + shop_id +"';";
  388.                   Statement statement2 = c.createStatement();
  389.                   ResultSet res2 = statement2.executeQuery(itemselect);
  390.                   Boolean noitems = true;
  391.        
  392.                   while(res2.next()) {
  393.                       noitems = false;
  394.                       p.sendMessage(intro + ChatColor.GRAY + "   - " + ChatColor.GREEN + "" + ChatColor.BOLD + "x" + res2.getString("itemamount") + " " + res2.getString("itemname") + ChatColor.GOLD + " For " + ChatColor.GREEN + ChatColor.BOLD + res2.getString("itemprice") + ChatColor.GOLD + " Diamond(s)!");
  395.                   }
  396.                   if (noitems) {
  397.                       p.sendMessage(intro + ChatColor.GRAY + "   -" + ChatColor.RED + " Shop has no items!");
  398.                   }
  399.               }
  400.             } catch (SQLException e) {
  401.               // TODO Auto-generated catch block
  402.               e.printStackTrace();
  403.             }
  404.           } else {
  405.             p.sendMessage(intro + ChatColor.RED + "Sorry, you do not have permission to list all shops");
  406.           }
  407.       }
  408.  }
  409.  } else {
  410.      console.sendMessage(ChatColor.DARK_RED + "[Critical Warning]" + ChatColor.RED + " This Is For Players Only");
  411.  }
  412.     return true;
  413.   }
  414. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement