Advertisement
Guest User

Lootbox 1.4

a guest
Feb 1st, 2015
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 56.20 KB | None | 0 0
  1. package me.TheOatBaron.Bukkit;
  2.  
  3. import java.io.File;
  4. import java.util.ArrayList;
  5. import java.util.Iterator;
  6. import java.util.LinkedList;
  7. import java.util.Map.Entry;
  8. import java.util.Random;
  9. import java.util.logging.Logger;
  10.  
  11. import org.bukkit.Bukkit;
  12. import org.bukkit.ChatColor;
  13. import org.bukkit.Material;
  14. import org.bukkit.Sound;
  15. import org.bukkit.command.Command;
  16. import org.bukkit.command.CommandSender;
  17. import org.bukkit.enchantments.Enchantment;
  18. import org.bukkit.entity.Player;
  19. import org.bukkit.event.EventHandler;
  20. import org.bukkit.event.Listener;
  21. import org.bukkit.event.block.Action;
  22. import org.bukkit.event.inventory.InventoryClickEvent;
  23. import org.bukkit.event.player.PlayerInteractEvent;
  24. import org.bukkit.inventory.Inventory;
  25. import org.bukkit.inventory.ItemStack;
  26. import org.bukkit.inventory.meta.ItemMeta;
  27. import org.bukkit.plugin.java.JavaPlugin;
  28.  
  29. //
  30. //As a note to the Bukkit Staff:
  31. //
  32. //I'm sorry if this plugin causes anyone to be diagnosed with PTSD after going through it.
  33. //
  34.  
  35. public class Lootbox extends JavaPlugin implements Listener{
  36.     Logger log = Bukkit.getServer().getLogger();
  37.     Random rand;
  38.     @Override
  39.     public void onEnable(){
  40.         getServer().getPluginManager().registerEvents(this, this);
  41.         if (!getDataFolder().exists()){
  42.             getDataFolder().mkdir();
  43.         }
  44.         File file = new File(getDataFolder(), "config.yml");
  45.         if(!file.exists()){
  46.             this.saveDefaultConfig();
  47.         }
  48.         if(!getConfig().contains("options")){
  49.             getConfig().set("options.chat.prefix", "&7[&6Lootbox&7] ");
  50.             getConfig().set("options.chat.no-permissions", "&7You do not have permission to do this!");
  51.             getConfig().set("options.chat.improper-usage-of-command", "&7Improper usage of the command!");
  52.             getConfig().set("options.chat.empty-lootbox", "&7The lootbox appears to be empty...");
  53.             getConfig().set("options.chat.more-than-one-lootbox", "&7You can only have ONE lootbox in your hand while opening it!");
  54.             getConfig().set("options.chat.more-than-one-key", "&7You can only have ONE key in your hand while using it!");
  55.             getConfig().set("options.chat.full-inventory", "&7Your inventory is too full for this!");
  56.             getConfig().set("options.chat.config-error", "&7Something went wrong in the Lootbox config.yml :C");
  57.             getConfig().set("options.chat.reload", "&7Lootbox config.yml has been reloaded!");
  58.             getConfig().set("options.chat.given-lootbox", "&7You have been given a lootbox!");
  59.             getConfig().set("options.chat.gave-lootbox", "&7You have gave a lootbox!");
  60.             getConfig().set("options.chat.given-key", "&7You have been given a key!");
  61.             getConfig().set("options.chat.gave-key", "&7You have gave a key!");
  62.             getConfig().set("options.chat.lootbox-given", "&7You have been given a lootbox!");
  63.             getConfig().set("options.chat.no-key", "&7This box requires a key to open! If you have the proper one, right click with it!");
  64.             getConfig().set("options.chat.nothing", "&7This key does not open anything!");
  65.             getConfig().set("options.chat.no-lootbox-to-open", "&7This key can't open any lootboxes you have, make sure they aren't stacked!");
  66.             getConfig().set("options.chat.no-exist", "&7That lootbox/key doesn't exist! Remember, names are case-sensitive!");
  67.             getConfig().set("options.chat.offline-player", "&7That player is offline!");
  68.             getConfig().set("options.items.lootbox-item", "54");
  69.             getConfig().set("options.items.key-item", "131");
  70.             getConfig().set("options.other.make-noise-when-lootbox-opens", true);
  71.         }
  72.         //v1.2 Support
  73.         if(!getConfig().contains("options.chat.no-key-needed")){
  74.             getConfig().set("options.chat.seperator", "&7----------------");
  75.             getConfig().set("options.chat.lootboxes", "&8Lootboxes");
  76.             getConfig().set("options.chat.keys", "&8Keys");
  77.             getConfig().set("options.chat.improper-item", "&7You aren't holding the proper item!");
  78.             getConfig().set("options.chat.no-key-needed", "&8No key needed to open this lootbox!");
  79.             getConfig().set("options.chat.no-lootboxes", "&8This key doesn't open any lootbox!");
  80.         }
  81.         //v1.3 Support
  82.         if(!getConfig().contains("options.chat.lootbox-too-big")){
  83.             getConfig().set("options.chat.not-a-lootbox", "&7You are not holding a lootbox!");
  84.             getConfig().set("options.chat.not-a-key", "&7You are not holding a key!");
  85.             getConfig().set("options.chat.lootbox-too-big", "&7This lootbox contains too many items to open! Contact an administrator!");
  86.         }
  87.         //v1.4 Support
  88.         if(!getConfig().contains("options.chat.lootbox-created")){
  89.             getConfig().set("options.chat.editor-name-complete", "&7A name has been successfully added to the item!");
  90.             getConfig().set("options.chat.editor-lore-complete", "&7A line of lore has been successfully added to the item!");
  91.             getConfig().set("options.chat.editor-chance-complete", "&7A chance has been successfully added to the item!");
  92.             getConfig().set("options.chat.no-item", "&7You must be holding an item!");
  93.             getConfig().set("options.chat.empty-inventory", "&7You must have something in your inventory to do this! Lootbox not created.");
  94.             getConfig().set("options.chat.already-exists", "&7That lootbox/key already exists! Lootbox not created.");
  95.             getConfig().set("options.chat.permission-in-use", "&7That permission is already in use! Lootbox not created.");
  96.             getConfig().set("options.chat.how-to-create", "&7The first item in your inventory has to be a lootbox item and the second has to be a key item. They both need a name and lore. If you want a non-key lootbox, use '/lootbox create nokey' and have your first inventory slot as a lootbox item. Lootbox not created.");
  97.             getConfig().set("options.chat.lootbox-created", "&7You have created a lootbox!");
  98.         }      
  99.         if(!getConfig().contains("lootboxes")){
  100.             getConfig().createSection("lootboxes");
  101.         }
  102.         if(!getConfig().contains("keys")){
  103.             getConfig().createSection("keys");
  104.         }
  105.         getConfig().options().header("Lootbox v1.4 (TheOatBaron) - http://dev.bukkit.org/bukkit-plugins/lootbox/");
  106.         getConfig().options().copyHeader();
  107.         getConfig().options().copyDefaults(true);
  108.         saveConfig();
  109.         reloadConfig();
  110.     }
  111.     @SuppressWarnings({ "deprecation", "unchecked" })
  112.     @Override
  113.     public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
  114.         if(label.equalsIgnoreCase("lootbox")){
  115.             if(sender instanceof Player){
  116.                 Player initial = (Player) sender;
  117.                 if(args.length==0 || args[0].equalsIgnoreCase("help")){
  118.                     if(isPermissible(initial, "help", true)){
  119.                         initial.sendMessage(pre()+"§7Lootbox Help");
  120.                         initial.sendMessage(pre()+"§8----------------------");
  121.                         initial.sendMessage(pre()+"§7Right-clicking with a lootbox opens it! If it requires a key, put the lootbox you want to open in your '1' slot and right click with the corresponding key!");
  122.                         initial.sendMessage(pre()+"§7/lootbox help §0- §8Displays this help page.");
  123.                         if(isPermissible(initial, "preview", false)){
  124.                             initial.sendMessage(pre()+"§7/lootbox preview §0- §8Shows what you can get in a lootbox, excluding commands. (i.e. Money)");
  125.                         }
  126.                         if(isPermissible(initial, "keys", false)){
  127.                             initial.sendMessage(pre()+"§7/lootbox keys §0- §8Shows what keys can be used to open the lootbox in hand.");
  128.                         }
  129.                         if(isPermissible(initial, "lootboxes", false)){
  130.                             initial.sendMessage(pre()+"§7/lootbox lootboxes §0- §8Shows what lootboxes can be opened with the key in hand.");
  131.                         }
  132.                         if(isPermissible(initial, "info", false)){
  133.                             initial.sendMessage(pre()+"§7/lootbox info §0- §8Get information on an item.");
  134.                         }
  135.                         if(isPermissible(initial, "admin.reload", false)){
  136.                             initial.sendMessage(pre()+"§7/lootbox reload §0- §8Reloads the config.yml, save first.");
  137.                         }
  138.                         if(isPermissible(initial, "admin.list", false)){
  139.                             initial.sendMessage(pre()+"§7/lootbox list <permissions/lootboxes/keys> §0- §8Lists the commands/lootboxes/keys and their permission nodes.");
  140.                         }
  141.                         if(isPermissible(initial, "admin.editor", false)){
  142.                             initial.sendMessage(pre()+"§7/lootbox editor <name/lore/chance> <args> §0- §8Give an item a name/lore/chance.");
  143.                         }
  144.                         if(isPermissible(initial, "admin.give", false)){
  145.                             initial.sendMessage(pre()+"§7/lootbox give <username> <lootbox:key> <lootbox/key name> §0- §8Gives a player a lootbox/key.");
  146.                         }
  147.                         if(isPermissible(initial, "admin.create", false)){
  148.                             initial.sendMessage(pre()+"§7/lootbox create [nokey] §0- §8Create a lootbox. Visit the dev page (http://dev.bukkit.org/bukkit-plugins/lootbox/) for more info.");
  149.                         }
  150.                     }
  151.                 }else if(args[0].equalsIgnoreCase("give") && args.length > 3){
  152.                     if(isPermissible(initial, "admin.give", true)){
  153.                         if(Bukkit.getServer().getPlayer(args[1])==null){
  154.                             initial.sendMessage(pre()+getConfig().getString("options.chat.offline-player").replaceAll("&", "§"));
  155.                             log.info(pre() + initial.getName() + " tried to give " + args[1] + " a lootbox/key, but they were offline!");
  156.                         }else{
  157.                             Player target = Bukkit.getServer().getPlayer(args[1]);
  158.                             ItemStack is = getConfigItemStack("options.items.lootbox-item");
  159.                             ItemStack is2 = getConfigItemStack("options.items.key-item");
  160.                             if(is!=null && is2!=null){
  161.                                 String dispName = "";
  162.                                 for(int i=3;i<args.length;i++){
  163.                                     if(i==3){
  164.                                         dispName = dispName + args[i];
  165.                                     }else{
  166.                                         dispName = dispName + " " + args[i];
  167.                                     }
  168.                                 }
  169.                                 dispName = ChatColor.stripColor(dispName.replaceAll(" ", "_").replaceAll("&", "§"));
  170.                                 if(args[2].equalsIgnoreCase("lootbox") || args[2].equalsIgnoreCase("box") || args[2].equalsIgnoreCase("chest")){
  171.                                     if(getConfig().contains("lootboxes."+dispName)){
  172.                                         ItemMeta im = is.getItemMeta();
  173.                                         im.setDisplayName(getConfig().getString("lootboxes."+dispName+".color").replaceAll("&", "§") + dispName.replaceAll("_", " "));
  174.                                         im.setLore(getConfigLore(getConfig().getString("lootboxes."+dispName+".lore"), false));
  175.                                         is.setItemMeta(im);
  176.                                         initial.sendMessage(pre()+getConfig().getString("options.chat.gave-lootbox").replaceAll("&", "§"));
  177.                                         target.sendMessage(pre()+getConfig().getString("options.chat.given-lootbox").replaceAll("&", "§"));
  178.                                         if(target.getInventory().firstEmpty()==-1){
  179.                                             target.sendMessage(pre()+getConfig().getString("options.chat.full-inventory").replaceAll("&", "§"));
  180.                                             Bukkit.getServer().getWorld(target.getWorld().getName()).dropItem(target.getLocation(), is);
  181.                                             log.info(pre() + initial.getName() + " has given " + target.getName() + " a lootbox, but they had no room so the item dropped at their feet.");
  182.                                         }else{
  183.                                             target.getInventory().addItem(is);
  184.                                             log.info(pre() + initial.getName() + " has given " + target.getName() + " a lootbox.");
  185.                                         }
  186.                                     }else{
  187.                                         initial.sendMessage(pre()+getConfig().getString("options.chat.no-exist").replaceAll("&", "§"));
  188.                                     }
  189.                                 }else if(args[2].equalsIgnoreCase("key")){
  190.                                     if(getConfig().contains("keys."+dispName)){
  191.                                         ItemMeta im = is2.getItemMeta();
  192.                                         im.setDisplayName(getConfig().getString("keys."+dispName+".color").replaceAll("&", "§") + dispName.replaceAll("_", " "));
  193.                                         im.setLore(getConfigLore(getConfig().getString("keys."+dispName+".lore"), false));
  194.                                         is2.setItemMeta(im);
  195.                                         initial.sendMessage(pre()+getConfig().getString("options.chat.gave-key").replaceAll("&", "§"));
  196.                                         target.sendMessage(pre()+getConfig().getString("options.chat.given-key").replaceAll("&", "§"));
  197.                                         if(target.getInventory().firstEmpty()==-1){
  198.                                             target.sendMessage(pre()+getConfig().getString("options.chat.full-inventory").replaceAll("&", "§"));
  199.                                             Bukkit.getServer().getWorld(target.getWorld().getName()).dropItem(target.getLocation(), is2);
  200.                                             log.info(pre() + initial.getName() + " has given " + target.getName() + " a key, but they had no room so the item dropped at their feet.");
  201.                                         }else{
  202.                                             target.getInventory().addItem(is2);
  203.                                             log.info(pre() + initial.getName() + " has given " + target.getName() + " a key.");
  204.                                         }
  205.                                     }else{
  206.                                         initial.sendMessage(pre()+getConfig().getString("options.chat.no-exist").replaceAll("&", "§"));
  207.                                     }
  208.                                 }else{
  209.                                     initial.sendMessage(pre()+getConfig().getString("options.chat.improper-usage-of-command").replaceAll("&", "§"));
  210.                                 }
  211.                             }else{
  212.                                 initial.sendMessage(pre()+getConfig().getString("options.chat.config-error").replaceAll("&", "§"));
  213.                             }
  214.                         }
  215.                     }
  216.                 }else if(args[0].equalsIgnoreCase("reload") || args[0].equalsIgnoreCase("save")){
  217.                     if(isPermissible(initial, "admin.reload", true)){
  218.                         reloadConfig();
  219.                         initial.sendMessage(pre()+getConfig().getString("options.chat.reload").replaceAll("&", "§"));
  220.                     }
  221.                 }else if(args[0].equalsIgnoreCase("keys") || args[0].equalsIgnoreCase("key")){
  222.                     if(isPermissible(initial, "keys", true)){
  223.                         ItemStack iih = initial.getItemInHand();
  224.                         if(isLootbox(iih)){
  225.                             ItemMeta iihm = iih.getItemMeta();
  226.                             String sn = ChatColor.stripColor(iihm.getDisplayName()).replaceAll(" ", "_");
  227.                             String rn = sn.replaceAll("_", " ");
  228.                             rn = getConfig().getString("lootboxes."+sn+".color").replaceAll("&", "§") + rn;
  229.                             initial.sendMessage(pre()+getConfig().getString("options.chat.keys").replaceAll("&", "§"));
  230.                             initial.sendMessage(pre()+getConfig().getString("options.chat.seperator").replaceAll("&", "§"));
  231.                             boolean nnk = true;
  232.                             for(String key : getConfig().getConfigurationSection("keys").getKeys(false)){
  233.                                 if(getConfig().getList("keys."+key+".opens-lootboxes").contains(sn)){
  234.                                     initial.sendMessage(pre()+(getConfig().getString("keys."+key+".color")+key).replaceAll("&", "§").replaceAll("_", " "));
  235.                                     nnk = false;
  236.                                 }
  237.                             }
  238.                             if(nnk){
  239.                                 initial.sendMessage(pre()+getConfig().getString("options.chat.no-key-needed").replaceAll("&", "§"));
  240.                             }
  241.                         }else{
  242.                             initial.sendMessage(pre()+getConfig().getString("options.chat.not-a-lootbox").replaceAll("&", "§"));
  243.                         }
  244.                     }
  245.                 }else if(args[0].equalsIgnoreCase("lootboxes") || args[0].equalsIgnoreCase("lootboxs") || args[0].equalsIgnoreCase("lootbox")){
  246.                     if(isPermissible(initial, "lootboxes", true)){
  247.                         ItemStack iih = initial.getItemInHand();
  248.                         if(isKey(iih)){
  249.                             ItemMeta iihm = iih.getItemMeta();
  250.                             String sn = ChatColor.stripColor(iihm.getDisplayName()).replaceAll(" ", "_");
  251.                             String rn = sn.replaceAll("_", " ");
  252.                             rn = getConfig().getString("keys."+sn+".color").replaceAll("&", "§") + rn;
  253.                             initial.sendMessage(pre()+getConfig().getString("options.chat.lootboxes").replaceAll("&", "§"));
  254.                             initial.sendMessage(pre()+getConfig().getString("options.chat.seperator").replaceAll("&", "§"));
  255.                             ArrayList<String> list = (ArrayList<String>) getConfig().getList("keys."+sn+".opens-lootboxes");
  256.                             boolean nnk=true;
  257.                             for(int i=0;i<list.size();i++){
  258.                                 if(getConfig().contains("lootboxes."+list.get(i))){
  259.                                     initial.sendMessage(pre()+(getConfig().getString("lootboxes."+list.get(i)+".color")+list.get(i)).replaceAll("&", "§").replaceAll("_", " "));
  260.                                     nnk = false;
  261.                                 }else{
  262.                                     log.warning(pre()+"Lootbox "+list.get(i)+" doesn't exist!");
  263.                                 }
  264.                             }
  265.                             if(nnk){
  266.                                 initial.sendMessage(pre()+getConfig().getString("options.chat.no-lootboxes").replaceAll("&", "§"));
  267.                             }
  268.                         }else{
  269.                             initial.sendMessage(pre()+getConfig().getString("options.chat.not-a-key").replaceAll("&", "§"));
  270.                         }
  271.                     }
  272.                 }else if(args[0].equalsIgnoreCase("editor")){
  273.                     if(isPermissible(initial, "admin.editor", true)){
  274.                         ItemStack iih = initial.getItemInHand();
  275.                         if(hasItemInHand(iih)){
  276.                             if(args.length>2 && args[1].equalsIgnoreCase("name")){
  277.                                 String name = args[2];
  278.                                 if(!name.startsWith("&")){
  279.                                     name="&7"+name;
  280.                                 }
  281.                                 for(int i=3;i<args.length;i++){
  282.                                     name = name+" "+args[i];
  283.                                 }
  284.                                 name = name.replaceAll("&", "§");
  285.                                 ItemMeta im = iih.getItemMeta();
  286.                                 im.setDisplayName(name);
  287.                                 iih.setItemMeta(im);
  288.                                 initial.sendMessage(pre()+getConfig().getString("options.chat.editor-name-complete").replaceAll("&", "§"));
  289.                             }else if(args.length>2 && args[1].equalsIgnoreCase("lore")){
  290.                                 ItemMeta im = iih.getItemMeta();
  291.                                 String name = args[2];
  292.                                 for(int i=3;i<args.length;i++){
  293.                                     name = name+" "+args[i];
  294.                                 }
  295.                                 name = name.replaceAll("&", "§");
  296.                                 ArrayList<String> lore = new ArrayList<String>();
  297.                                 if(im.hasLore()){
  298.                                     lore = (ArrayList<String>) im.getLore();
  299.                                 }
  300.                                 lore.add(name);
  301.                                 im.setLore(lore);
  302.                                 iih.setItemMeta(im);
  303.                                 initial.sendMessage(pre()+getConfig().getString("options.chat.editor-lore-complete").replaceAll("&", "§"));
  304.                             }else if(args.length==3 && args[1].equalsIgnoreCase("chance")){
  305.                                 ItemMeta im = iih.getItemMeta();
  306.                                 boolean go=true;
  307.                                 int chance = 100;
  308.                                 try{
  309.                                     chance = Integer.parseInt(args[2]);
  310.                                 }catch(NumberFormatException e){
  311.                                     go = false;
  312.                                 }
  313.                                 if(go && chance>-1 && chance<101){
  314.                                     ArrayList<String> lore = new ArrayList<String>();
  315.                                     if(im.hasLore()){
  316.                                         lore = (ArrayList<String>) im.getLore();
  317.                                     }
  318.                                     lore.add("§7"+chance+"% Chance");
  319.                                     im.setLore(lore);
  320.                                     iih.setItemMeta(im);
  321.                                     initial.sendMessage(pre()+getConfig().getString("options.chat.editor-chance-complete").replaceAll("&", "§"));
  322.                                 }else{
  323.                                     initial.sendMessage(pre()+getConfig().getString("options.chat.improper-usage-of-command").replaceAll("&", "§"));
  324.                                 }
  325.                             }else{
  326.                                 initial.sendMessage(pre()+getConfig().getString("options.chat.improper-usage-of-command").replaceAll("&", "§"));
  327.                             }
  328.                         }else{
  329.                             initial.sendMessage(pre()+getConfig().getString("options.chat.no-item").replaceAll("&", "§"));
  330.                         }
  331.                     }
  332.                 }else if(args[0].equalsIgnoreCase("list")){
  333.                     if(isPermissible(initial, "admin.list", true)){
  334.                         if(args.length==2){
  335.                             if(args[1].equalsIgnoreCase("permissions")){
  336.                                 initial.sendMessage(pre()+"§7Permission Nodes:");
  337.                                 initial.sendMessage(pre()+"§8 - /lootbox help - lootbox.help");
  338.                                 initial.sendMessage(pre()+"§8 - /lootbox preview - lootbox.preview");
  339.                                 initial.sendMessage(pre()+"§8 - /lootbox keys - lootbox.keys");
  340.                                 initial.sendMessage(pre()+"§8 - /lootbox lootboxes - lootbox.lootboxes");
  341.                                 initial.sendMessage(pre()+"§8 - /lootbox info - lootbox.info");
  342.                                 initial.sendMessage(pre()+"§8 - /lootbox editor <name/lore/chance> <args> - lootbox.admin.editor");
  343.                                 initial.sendMessage(pre()+"§8 - /lootbox list <permissions/keys/lootboxes> - lootbox.admin.list");
  344.                                 initial.sendMessage(pre()+"§8 - /lootbox create [nokey] - lootbox.admin.create");
  345.                                 initial.sendMessage(pre()+"§8 - /lootbox reload - lootbox.admin.reload");
  346.                                 initial.sendMessage(pre()+"§8 - /lootbox give - lootbox.admin.give");
  347.                                 initial.sendMessage(pre()+"§7Note: This does not include usage permissions, please see the following for usage permissions:");
  348.                                 initial.sendMessage(pre()+"§8 - /lootbox list keys");
  349.                                 initial.sendMessage(pre()+"§8 - /lootbox list lootboxes");
  350.                             }else if(args[1].equalsIgnoreCase("keys") || args[1].equalsIgnoreCase("key")){
  351.                                 initial.sendMessage(pre()+"§7Keys - Permission Nodes");
  352.                                 for(String key : getConfig().getConfigurationSection("keys").getKeys(false)){
  353.                                     initial.sendMessage(pre()+getConfig().getString("keys."+key+".color").replaceAll("&", "§")+key.replaceAll("_", " ")+" §8- §7" + "lootbox.use.key."+getConfig().getString("keys."+key+".permission"));
  354.                                 }
  355.                             }else if(args[1].equalsIgnoreCase("lootboxes") || args[1].equalsIgnoreCase("lootboxs") || args[1].equalsIgnoreCase("lootbox") || args[1].equalsIgnoreCase("boxes") || args[1].equalsIgnoreCase("boxs") || args[1].equalsIgnoreCase("box")){
  356.                                 initial.sendMessage(pre()+"§7Lootboxes - Permission Nodes");
  357.                                 for(String lootbox : getConfig().getConfigurationSection("lootboxes").getKeys(false)){
  358.                                     initial.sendMessage(pre()+getConfig().getString("lootboxes."+lootbox+".color").replaceAll("&", "§")+lootbox.replaceAll("_", " ")+" §8- §7" + "lootbox.use.lootbox."+getConfig().getString("lootboxes."+lootbox+".permission"));
  359.                                 }
  360.                             }else{
  361.                                 initial.sendMessage(pre()+getConfig().getString("options.chat.improper-usage-of-command").replaceAll("&", "§"));
  362.                             }
  363.                         }else{
  364.                             initial.sendMessage(pre()+getConfig().getString("options.chat.improper-usage-of-command").replaceAll("&", "§"));
  365.                         }
  366.                     }
  367.                 }else if(args[0].equalsIgnoreCase("info")){
  368.                     if(isPermissible(initial, "info", true)){
  369.                         ItemStack iih = initial.getItemInHand();
  370.                         if(hasItemInHand(iih)){
  371.                             ItemMeta im = iih.getItemMeta();
  372.                             initial.sendMessage("§7Lootbox Info");
  373.                             initial.sendMessage("§8-----------------");
  374.                             if(iih.getDurability()==0){
  375.                                 initial.sendMessage("§8ID: §7" + iih.getTypeId());
  376.                             }else{
  377.                                 initial.sendMessage("§8ID: §7" + iih.getTypeId()+":"+iih.getDurability());
  378.                             }
  379.                             if(im.hasDisplayName()){
  380.                                 initial.sendMessage("§8Name: §7" + im.getDisplayName());
  381.                             }
  382.                             if(im.hasLore()){
  383.                                 initial.sendMessage("§8Lore: ");
  384.                                 for(int i=0;i<im.getLore().size();i++){
  385.                                     initial.sendMessage(" §7- "+"§5§o"+im.getLore().get(i));
  386.                                 }
  387.                             }
  388.                             if(im.hasEnchants()){
  389.                                 initial.sendMessage("§8Enchantments: ");
  390.                                 Iterator<Entry<Enchantment, Integer>> it = iih.getEnchantments().entrySet().iterator();
  391.                                 while (it.hasNext()) {
  392.                                     Entry<Enchantment, Integer> pairs = it.next();
  393.                                     Enchantment ench = pairs.getKey();
  394.                                     initial.sendMessage("§7"+ench.getName()+" ("+ench.getId()+") - " + pairs.getValue() + " Levels");
  395.                                 }
  396.                             }
  397.                         }else{
  398.                             initial.sendMessage(pre()+getConfig().getString("options.chat.no-item").replaceAll("&", "§"));
  399.                         }
  400.                     }
  401.                 }else if(args[0].equalsIgnoreCase("create")){
  402.                     boolean reqkey=true;
  403.                     if(args.length==2 && args[1].equalsIgnoreCase("nokey")){
  404.                         reqkey=false;
  405.                     }
  406.                     if(isPermissible(initial, "admin.create", true)){
  407.                         Inventory iinv = initial.getInventory();
  408.                         ItemStack[] list = iinv.getContents();
  409.                         if(list.length!=0){
  410.                             int fid = 0;
  411.                             int sid = 0;
  412.                             boolean go = true;
  413.                             try{
  414.                                 fid = Integer.parseInt(getConfig().getString("options.items.lootbox-item"));
  415.                                 sid = Integer.parseInt(getConfig().getString("options.items.key-item"));
  416.                             }catch(NumberFormatException e){
  417.                                 go = false;
  418.                             }
  419.                             if(go){
  420.                                 boolean quickpass = false;
  421.                                 if(reqkey){
  422.                                     if(list[0]!=null && list[0].getTypeId()==fid&& list[0].hasItemMeta() && list[0].getItemMeta().hasLore() && list[0].getItemMeta().hasDisplayName() && list[1]!=null&& list[1].getTypeId()==sid&& list[1].hasItemMeta() && list[1].getItemMeta().hasLore() && list[1].getItemMeta().hasDisplayName()){
  423.                                         quickpass = true;
  424.                                     }
  425.                                 }else if(list[0]!=null&& list[0].getTypeId()==fid&& list[0].hasItemMeta() && list[0].getItemMeta().hasLore() && list[0].getItemMeta().hasDisplayName()){
  426.                                     quickpass = true;
  427.                                 }
  428.                                 if(quickpass){
  429.                                     ArrayList<String> flore = (ArrayList<String>) list[0].getItemMeta().getLore();
  430.                                     ArrayList<String> slore = new ArrayList<String>();
  431.                                     String sname = "";
  432.                                     String suname = "";
  433.                                     if(reqkey){
  434.                                         slore = (ArrayList<String>) list[1].getItemMeta().getLore();
  435.                                     }
  436.                                     String fname = list[0].getItemMeta().getDisplayName();
  437.                                     if(reqkey){
  438.                                         sname = list[1].getItemMeta().getDisplayName();
  439.                                     }
  440.                                     String funame = ChatColor.stripColor(list[0].getItemMeta().getDisplayName().replaceAll(" ", "_"));
  441.                                     if(reqkey){
  442.                                         suname = ChatColor.stripColor(list[1].getItemMeta().getDisplayName().replaceAll(" ", "_"));
  443.                                     }
  444.                                     if(getConfig().contains("lootboxes."+funame) || (reqkey && getConfig().contains("keys."+suname))){
  445.                                         initial.sendMessage(pre()+getConfig().getString("options.chat.already-exists").replaceAll("&", "§")); 
  446.                                     }else{
  447.                                         String fpath = "lootboxes."+funame;
  448.                                         String spath = "";
  449.                                         if(reqkey){
  450.                                             spath = "keys."+suname;
  451.                                         }
  452.                                         getConfig().createSection(fpath);
  453.                                         if(reqkey){
  454.                                             getConfig().createSection(spath);
  455.                                         }
  456.                                         String fcolor = "&f&o";
  457.                                         String scolor = "&f&o";
  458.                                         if(fname.startsWith("§")){
  459.                                             fcolor = "&"+fname.charAt(1);
  460.                                         }
  461.                                         if(reqkey && sname.startsWith("§")){
  462.                                             scolor = "&"+sname.charAt(1);
  463.                                         }
  464.                                         getConfig().set(fpath+".color", fcolor);
  465.                                         if(reqkey){
  466.                                             getConfig().set(spath+".color", scolor);
  467.                                         }
  468.                                         String fstr = flore.get(0).replaceAll("§","&");
  469.                                         String sstr = "";
  470.                                         if(reqkey){
  471.                                             sstr = slore.get(0).replaceAll("§","&");
  472.                                         }
  473.                                         for(int i=1;i<flore.size();i++){
  474.                                             fstr = fstr+"|"+flore.get(i).replaceAll("§","&");
  475.                                         }
  476.                                         getConfig().set(fpath+".lore", fstr);
  477.                                         if(reqkey){
  478.                                             for(int i=1;i<slore.size();i++){
  479.                                                 sstr = sstr+"|"+slore.get(i).replaceAll("§","&");
  480.                                             }
  481.                                         }
  482.                                         if(reqkey){
  483.                                             getConfig().set(spath+".lore", sstr);
  484.                                         }
  485.                                         boolean sgo=true;
  486.                                         for(String box : getConfig().getConfigurationSection("lootboxes").getKeys(false)){
  487.                                             if(!box.equals(funame) && getConfig().getString("lootboxes."+box+".permission").equalsIgnoreCase(funame)){
  488.                                                 sgo = false;
  489.                                             }
  490.                                         }
  491.                                         if(reqkey){
  492.                                             for(String key : getConfig().getConfigurationSection("keys").getKeys(false)){
  493.                                                 if(!key.equals(suname) && getConfig().getString("keys."+key+".permission").equalsIgnoreCase(suname)){
  494.                                                     sgo = false;
  495.                                                 }
  496.                                             }
  497.                                         }
  498.                                         if(sgo){
  499.                                             getConfig().set(fpath+".permission", funame.toLowerCase());
  500.                                             if(reqkey){
  501.                                                 getConfig().set(fpath+".requires-a-key", true);
  502.                                                 getConfig().set(spath+".permission", suname.toLowerCase());
  503.                                             }else{
  504.                                                 getConfig().set(fpath+".requires-a-key", false);
  505.                                             }
  506.                                             ArrayList<String> newList = new ArrayList<String>();
  507.                                             newList.add(funame);
  508.                                             if(reqkey){
  509.                                                 getConfig().set(spath+".opens-lootboxes", newList);
  510.                                             }
  511.                                             initial.sendMessage(pre()+getConfig().getString("options.chat.lootbox-created").replaceAll("&", "§"));
  512.                                             ArrayList<String> allItems = new ArrayList<String>();
  513.                                             if(reqkey){
  514.                                                 for(int i=2;i<iinv.getSize();i++){
  515.                                                     ItemStack item = iinv.getItem(i);
  516.                                                     if(item!=null && item.getType()!=Material.AIR){
  517.                                                         allItems.add(toLootboxString(item));   
  518.                                                     }
  519.                                                 }
  520.                                             }else{
  521.                                                 for(int i=1;i<iinv.getSize();i++){
  522.                                                     ItemStack item = iinv.getItem(i);
  523.                                                     if(item!=null && item.getType()!=Material.AIR){
  524.                                                         allItems.add(toLootboxString(item));   
  525.                                                     }
  526.                                                 }
  527.                                             }
  528.                                             getConfig().set(fpath+".items", allItems);
  529.                                             saveConfig();
  530.                                             reloadConfig();
  531.                                         }else{
  532.                                             initial.sendMessage(pre()+getConfig().getString("options.chat.how-to-create").replaceAll("&", "§"));
  533.                                             getConfig().set(fpath, null);
  534.                                             if(reqkey){
  535.                                                 getConfig().set(spath, null);
  536.                                             }
  537.                                         }
  538.                                     }
  539.                                 }else{
  540.                                     initial.sendMessage(pre()+getConfig().getString("options.chat.how-to-create").replaceAll("&", "§"));  
  541.                                 }
  542.                             }else{
  543.                                 initial.sendMessage(pre()+getConfig().getString("options.chat.config-error").replaceAll("&", "§"));
  544.                             }
  545.                         }else{
  546.                             initial.sendMessage(pre()+getConfig().getString("options.chat.empty-inventory").replaceAll("&", "§"));
  547.                         }
  548.                     }
  549.                 }else if(args[0].equalsIgnoreCase("preview")){
  550.                     if(isPermissible(initial, "preview", true)){
  551.                         ItemStack iih = initial.getItemInHand();
  552.                         if(isLootbox(iih)){
  553.                             ItemMeta iihm = iih.getItemMeta();
  554.                             String sn = ChatColor.stripColor(iihm.getDisplayName()).replaceAll(" ", "_");
  555.                             String rn = sn.replaceAll("_", " ");
  556.                             rn = getConfig().getString("lootboxes."+sn+".color").replaceAll("&", "§") + rn;
  557.                             if(isLootbox(iih)){
  558.                                 ArrayList<String> list = (ArrayList<String>) getConfig().getList("lootboxes."+sn+".items");
  559.                                 if(list.isEmpty()){
  560.                                     initial.sendMessage(pre()+getConfig().getString("options.chat.empty-lootbox").replaceAll("&", "§"));
  561.                                 }else{
  562.                                     String path = ChatColor.stripColor(iih.getItemMeta().getDisplayName().replaceAll(" ", "_"));
  563.                                     ArrayList<ItemStack> items = getLootboxItems(path);
  564.                                     Inventory prev = Bukkit.getServer().createInventory(null, 45, "Lootbox Preview");
  565.                                     if(canFitItems(prev, items)){
  566.                                         for(int i10=0;i10<9;i10++){
  567.                                             prev.setItem(i10, iih);
  568.                                         }
  569.                                         ArrayList<Integer> chances = getLootboxChance(path);
  570.                                         for(int i=0;i<items.size();i++){
  571.                                             ItemStack is = items.get(i);
  572.                                             if(is.hasItemMeta() && is.getItemMeta().hasLore()){
  573.                                                 ItemMeta im = is.getItemMeta();
  574.                                                 ArrayList<String> lore = (ArrayList<String>) im.getLore();
  575.                                                 lore.add("§7"+chances.get(i)+"% Chance");
  576.                                                 im.setLore(lore);
  577.                                                 is.setItemMeta(im);
  578.                                                 if(chances.get(i)!=0){
  579.                                                     prev.addItem(is);
  580.                                                 }
  581.                                             }else{
  582.                                                 if(!is.hasItemMeta()){
  583.                                                     is.setItemMeta(null);
  584.                                                 }
  585.                                                 ItemMeta im = is.getItemMeta();
  586.                                                 ArrayList<String> lore = new ArrayList<String>();
  587.                                                 lore.add("§7"+chances.get(i)+"% Chance");
  588.                                                 im.setLore(lore);
  589.                                                 is.setItemMeta(im);
  590.                                                 if(chances.get(i)!=0){
  591.                                                     prev.addItem(is);
  592.                                                 }
  593.                                             }
  594.                                         }
  595.                                         initial.openInventory(prev);
  596.                                     }else{
  597.                                         initial.sendMessage(pre()+getConfig().getString("options.chat.lootbox-too-big").replaceAll("&", "§"));
  598.                                     }
  599.                                 }
  600.                             }
  601.                         }else{
  602.                             initial.sendMessage(pre()+getConfig().getString("options.chat.not-a-lootbox").replaceAll("&", "§"));
  603.                         }
  604.                     }
  605.                 }else{
  606.                     initial.sendMessage(pre()+getConfig().getString("options.chat.improper-usage-of-command").replaceAll("&", "§"));
  607.                 }
  608.             }else{
  609.                 if(args.length==1 && args[0].equalsIgnoreCase("reload")){
  610.                     reloadConfig();
  611.                     log.info(pre() + "config.yml reloaded!");
  612.                 }else if(args[0].equalsIgnoreCase("give") && args.length > 3){
  613.                     if(Bukkit.getServer().getPlayer(args[1])==null){
  614.                         log.info(pre() + "Server console tried to give " + args[1] + " a lootbox/key, but they were offline!");
  615.                     }else{
  616.                         Player target = Bukkit.getServer().getPlayer(args[1]);
  617.                         ItemStack is = getConfigItemStack("options.items.lootbox-item");
  618.                         ItemStack is2 = getConfigItemStack("options.items.key-item");
  619.                         if(is!=null && is2!=null){
  620.                             String dispName = "";
  621.                             for(int i=3;i<args.length;i++){
  622.                                 if(i==3){
  623.                                     dispName = dispName + args[i];
  624.                                 }else{
  625.                                     dispName = dispName + " " + args[i];
  626.                                 }
  627.                             }
  628.                             dispName = ChatColor.stripColor(dispName.replaceAll(" ", "_").replaceAll("&", "§"));
  629.                             if(args[2].equalsIgnoreCase("lootbox") || args[2].equalsIgnoreCase("box") || args[2].equalsIgnoreCase("chest")){
  630.                                 if(getConfig().contains("lootboxes."+dispName)){
  631.                                     ItemMeta im = is.getItemMeta();
  632.                                     im.setDisplayName(getConfig().getString("lootboxes."+dispName+".color").replaceAll("&", "§") + dispName.replaceAll("_", " "));
  633.                                     im.setLore(getConfigLore(getConfig().getString("lootboxes."+dispName+".lore"), false));
  634.                                     is.setItemMeta(im);
  635.                                     target.sendMessage(pre()+getConfig().getString("options.chat.given-lootbox").replaceAll("&", "§"));
  636.                                     if(target.getInventory().firstEmpty()==-1){
  637.                                         target.sendMessage(pre()+getConfig().getString("options.chat.full-inventory").replaceAll("&", "§"));
  638.                                         Bukkit.getServer().getWorld(target.getWorld().getName()).dropItem(target.getLocation(), is);
  639.                                         log.info(pre() + "Server console has given " + target.getName() + " a lootbox, but they had no room so the item dropped at their feet.");
  640.                                     }else{
  641.                                         target.getInventory().addItem(is);
  642.                                         log.info(pre() + "Server console has given " + target.getName() + " a lootbox.");
  643.                                     }
  644.                                 }else{
  645.                                     log.info(pre() + "Server console incorrectly executed the '/lootbox give' command, lootbox not given.");
  646.                                 }
  647.                             }else if(args[2].equalsIgnoreCase("key")){
  648.                                 if(getConfig().contains("keys."+dispName)){
  649.                                     ItemMeta im = is2.getItemMeta();
  650.                                     im.setDisplayName(getConfig().getString("keys."+dispName+".color").replaceAll("&", "§") + dispName.replaceAll("_", " "));
  651.                                     im.setLore(getConfigLore(getConfig().getString("keys."+dispName+".lore"), false));
  652.                                     is2.setItemMeta(im);
  653.                                     target.sendMessage(pre()+getConfig().getString("options.chat.given-key").replaceAll("&", "§"));
  654.                                     if(target.getInventory().firstEmpty()==-1){
  655.                                         target.sendMessage(pre()+getConfig().getString("options.chat.full-inventory").replaceAll("&", "§"));
  656.                                         Bukkit.getServer().getWorld(target.getWorld().getName()).dropItem(target.getLocation(), is2);
  657.                                         log.info(pre() + "Server console has given " + target.getName() + " a key, but they had no room so the item dropped at their feet.");
  658.                                     }else{
  659.                                         target.getInventory().addItem(is2);
  660.                                         log.info(pre() + "Server console has given " + target.getName() + " a key.");
  661.                                     }
  662.                                 }else{
  663.                                     log.info(pre() + "Server console incorrectly executed the '/lootbox give' command, lootbox not given.");
  664.                                 }
  665.                             }else{
  666.                                 log.info(pre() + "Server console incorrectly executed the '/lootbox give' command, lootbox not given.");
  667.                             }
  668.                         }else{
  669.                             log.info(pre() + "Server console ran into a config.yml error while executing '/lootbox give' command, lootbox not given.");
  670.                         }
  671.                     }
  672.                 }else{
  673.                     log.warning(pre() + "Server console tried to send a command, but it was not properly used!");
  674.                 }
  675.             }
  676.         }
  677.         return true;
  678.     }
  679.     @SuppressWarnings("deprecation")
  680.     public String toLootboxString(ItemStack item) {
  681.         //  id, amn, eids, name, lore, chance
  682.         if(item!=null && item.getType()!=Material.AIR){
  683.             String fullString = "";
  684.             if(item.getDurability()==0){
  685.                 fullString = fullString+item.getTypeId();
  686.             }else{
  687.                 fullString = fullString+item.getTypeId()+":"+item.getDurability();
  688.             }
  689.             fullString = fullString+" "+item.getAmount();
  690.             int chance = -1;
  691.             if(item.hasItemMeta()){
  692.                 ItemMeta im = item.getItemMeta();
  693.                 if(im.hasLore()){
  694.                     String loreString = "";
  695.                     ArrayList<String> lore = (ArrayList<String>) im.getLore();
  696.                     if(lore.get(0).startsWith("§7") && lore.get(0).endsWith("% Chance")){
  697.                         String[] split = lore.get(0).split("%");
  698.                         String tempString = split[0].replaceAll("§7", "");
  699.                         boolean go=true;
  700.                         try{
  701.                             chance = Integer.parseInt(tempString);
  702.                         }catch(NumberFormatException e){
  703.                             go = false;
  704.                         }
  705.                         if(!go || chance>100 || chance <0){
  706.                             chance = -1;
  707.                         }
  708.                     }else{
  709.                         loreString = lore.get(0).replaceAll("§", "&").replaceAll(" ", "_");
  710.                     }
  711.                     for(int i=1;i<lore.size();i++){
  712.                         if(lore.get(i).startsWith("§7") && lore.get(i).endsWith("% Chance")){
  713.                             String[] split = lore.get(i).split("%");
  714.                             String tempString = split[0].replaceAll("§7", "");
  715.                             boolean go=true;
  716.                             try{
  717.                                 chance = Integer.parseInt(tempString);
  718.                             }catch(NumberFormatException e){
  719.                                 go = false;
  720.                             }
  721.                             if(!go || chance>100 || chance <0){
  722.                                 chance = -1;
  723.                             }
  724.                         }else{
  725.                             if(loreString==""){
  726.                                 loreString = lore.get(i).replaceAll("§", "&").replaceAll(" ", "_");
  727.                             }else{
  728.                                 loreString = loreString+"|"+lore.get(i).replaceAll("§", "&").replaceAll(" ", "_");
  729.                             }
  730.                         }
  731.                     }
  732.                     if(loreString!=""){
  733.                         fullString = fullString+" lore:"+loreString.replaceAll(" ", "_");
  734.                     }
  735.                     if(chance!=-1){
  736.                         fullString = fullString+" chance:"+chance;
  737.                     }
  738.                 }
  739.                 if(im.hasDisplayName()){
  740.                     fullString = fullString+" name:"+im.getDisplayName().replaceAll("§", "&").replaceAll(" ", "_");
  741.                 }
  742.                 if(im.hasEnchants()){
  743.                     Iterator<Entry<Enchantment, Integer>> it = im.getEnchants().entrySet().iterator();
  744.                     while (it.hasNext()) {
  745.                         Entry<Enchantment, Integer> pairs = it.next();
  746.                         fullString = fullString+" "+pairs.getKey().getId()+":"+pairs.getValue();
  747.                     }
  748.                 }
  749.             }
  750.             return fullString;
  751.         }else{
  752.             return null;
  753.         }
  754.     }
  755.     @EventHandler
  756.     public void onInventoryClick(InventoryClickEvent event){
  757.         if(event.getInventory().getName().equals("Lootbox Preview")){
  758.             event.setCancelled(true);
  759.         }
  760.     }
  761.     @SuppressWarnings({ "unchecked", "unused" })
  762.     @EventHandler
  763.     public void onPlayerInteract(PlayerInteractEvent event){
  764.         if(event.getAction() == Action.RIGHT_CLICK_BLOCK || event.getAction() == Action.RIGHT_CLICK_AIR){
  765.             ItemStack iih = event.getPlayer().getItemInHand();
  766.             if(isKey(iih)){
  767.                 ItemMeta iihm = iih.getItemMeta();
  768.                 String sn = ChatColor.stripColor(iihm.getDisplayName()).replaceAll(" ", "_");
  769.                 if(event.getItem().getAmount()==1){
  770.                     String rn = sn.replaceAll("_", " ");
  771.                     rn = getConfig().getString("keys."+sn+".color").replaceAll("&", "§") + rn;
  772.                     ArrayList<String> testLore = (ArrayList<String>) iihm.getLore();
  773.                     ArrayList<String> configLore = getConfigLore(getConfig().getString("keys."+sn+".lore"), false);
  774.                     if(testLore.equals(configLore) && rn.equalsIgnoreCase(iihm.getDisplayName())){
  775.                         event.setCancelled(true);
  776.                         if(isPermissible(event.getPlayer(), "use.key." + getConfig().getString("keys."+sn+".permission"), true)){
  777.                             ArrayList<String> n = (ArrayList<String>) getConfig().getList("keys."+sn+".opens-lootboxes");
  778.                             if(n==null || n.size()==0){
  779.                                 event.getPlayer().sendMessage(pre()+getConfig().getString("options.chat.nothing").replaceAll("&", "§"));
  780.                             }else{
  781.                                 Inventory inv = event.getPlayer().getInventory();
  782.                                 ItemStack stack = new ItemStack(getConfigItemStack("options.items.lootbox-item"));
  783.                                 String save = "";
  784.                                 int slot=0;
  785.                                 boolean hasLB=false;
  786.                                 for(int i2=0;i2<inv.getSize();i2++){
  787.                                     if(!hasLB){
  788.                                         if(inv.getItem(i2)!=null && inv.getItem(i2).getType().equals(stack.getType()) && inv.getItem(i2).getAmount()==1){
  789.                                             if(inv.getItem(i2).getItemMeta().hasLore() && inv.getItem(i2).getItemMeta().hasDisplayName()){
  790.                                                 for(int i3=0;i3<n.size();i3++){
  791.                                                     if(n.get(i3).equals(ChatColor.stripColor(inv.getItem(i2).getItemMeta().getDisplayName().replaceAll(" ", "_"))) && inv.getItem(i2).getItemMeta().getLore().equals(getConfigLore(getConfig().getString("lootboxes."+n.get(i3)+".lore"), true))){
  792.                                                         Player p = event.getPlayer();
  793.                                                         ArrayList<String> list = (ArrayList<String>) getConfig().getList("lootboxes."+n.get(i3)+".items");
  794.                                                         if(getConfig().getList("lootboxes."+n.get(i3)+".items")==null || canFitItems(p.getInventory(), getLootboxItems(n.get(i3)))){
  795.                                                             save=n.get(i3);
  796.                                                             slot = i2;
  797.                                                             hasLB=true;
  798.                                                         }else{
  799.                                                             //TODO Rename? Could be sent multiple times
  800.                                                             p.sendMessage(pre()+getConfig().getString("options.chat.full-inventory").replaceAll("&", "§"));
  801.                                                         }
  802.                                                     }
  803.                                                 }
  804.                                             }
  805.                                         }
  806.                                     }
  807.                                 }
  808.                                 if(hasLB){
  809.                                     String path = save;
  810.                                     Player p = event.getPlayer();
  811.                                     p.getInventory().setItem(slot, null);
  812.                                     openLootbox(event.getPlayer(), save);
  813.                                 }else{
  814.                                     event.getPlayer().sendMessage(pre()+getConfig().getString("options.chat.no-lootbox-to-open").replaceAll("&", "§"));
  815.                                 }
  816.                             }
  817.                         }
  818.                     }
  819.                 }else{
  820.                     event.getPlayer().sendMessage(pre()+getConfig().getString("options.chat.more-than-one-key").replaceAll("&", "§"));
  821.                 }
  822.             }else if(isLootbox(iih)){
  823.                 ItemMeta iihm = iih.getItemMeta();
  824.                 String sn = ChatColor.stripColor(iihm.getDisplayName()).replaceAll(" ", "_");
  825.                 event.setCancelled(true);
  826.                 String rn = sn.replaceAll("_", " ");
  827.                 rn = getConfig().getString("lootboxes."+sn+".color").replaceAll("&", "§") + rn;
  828.                 if(getConfig().getBoolean("lootboxes."+sn+".requires-a-key")){
  829.                     event.getPlayer().sendMessage(pre()+getConfig().getString("options.chat.no-key").replaceAll("&", "§"));
  830.                 }else if(isPermissible(event.getPlayer(), "use.lootbox." + getConfig().getString("lootboxes."+sn+".permission"), true)){
  831.                     openLootbox(event.getPlayer(), sn);
  832.                 }
  833.             }
  834.         }
  835.     }
  836.     @SuppressWarnings({ "deprecation", "unchecked" })
  837.     private void openLootbox(Player p, String path){
  838.         //Check lists
  839.         if(p.getItemInHand().getAmount()==1){
  840.             //Check permissions
  841.             if(isPermissible(p, "use.lootbox."+getConfig().getString("lootboxes."+path+".permission"), true)){
  842.                 //Check if no items
  843.                 if(getConfig().getList("lootboxes."+path+".items")!=null && getConfig().getList("lootboxes."+path+".items").isEmpty()){
  844.                     p.sendMessage(pre()+getConfig().getString("options.chat.empty-lootbox").replaceAll("&", "§"));
  845.                 }else{
  846.                     ArrayList<ItemStack> items = getLootboxItems(path);
  847.                     if(getConfig().getList("lootboxes."+path+".items")==null || canFitItems(p.getInventory(), items)){
  848.                         p.getInventory().setItemInHand(new ItemStack(Material.AIR));
  849.                         //execute-commands from lootbox config section
  850.                         if(getConfig().getList("lootboxes."+path+".execute-commands")!=null){
  851.                             ArrayList<String> eC = (ArrayList<String>) getConfig().getList("lootboxes."+path+".execute-commands");
  852.                             execComm(p, eC);
  853.                         }
  854.                         if(getConfig().getString("lootboxes."+path+".open-message")!=null){
  855.                             p.sendMessage(pre()+getConfig().getString("lootboxes."+path+".open-message").replaceAll("&", "§"));
  856.                         }
  857.                         if(getConfig().getString("lootboxes."+path+".broadcast-message")!=null){
  858.                             Bukkit.getServer().broadcastMessage(pre()+getConfig().getString("lootboxes."+path+".broadcast-message").replaceAll("&", "§").replaceAll("\\{PLAYERNAME}", p.getName()));
  859.                         }
  860.                         if(Boolean.parseBoolean(getConfig().getString("options.other.make-noise-when-lootbox-opens"))){
  861.                             p.playSound(p.getLocation(), Sound.FIREWORK_LAUNCH, 1,0);
  862.                         }
  863.                         if(getConfig().getList("lootboxes."+path+".items")!=null){
  864.                             ArrayList<Integer> chances = getLootboxChance(path);
  865.                             ArrayList<ItemStack> mustItems = new ArrayList<ItemStack>();
  866.                             ArrayList<ItemStack> otherItems = new ArrayList<ItemStack>();
  867.                             ArrayList<ItemStack> leastItems = new ArrayList<ItemStack>();
  868.                             for(int i=0;i<items.size();i++){
  869.                                 int chance = chances.get(i);
  870.                                 rand = new Random();
  871.                                 int num = rand.nextInt(100)+1;
  872.                                 if(chance == 100){
  873.                                     otherItems.add(items.get(i));
  874.                                 }else if(chance >= num){
  875.                                     mustItems.add(items.get(i));
  876.                                 }else{
  877.                                     leastItems.add(items.get(i));
  878.                                 }
  879.                             }
  880.                             if(getMaxItems(path)==-1 || getMinItems(path)==-1){
  881.                                 p.sendMessage(pre()+getConfig().getString("options.chat.config-error").replaceAll("&", "§"));
  882.                             }else{
  883.                                 ArrayList<ItemStack> giveItems = getItemsToGive(getMinItems(path), getMaxItems(path), mustItems, otherItems, leastItems);
  884.                                 for(int i=0;i<giveItems.size();i++){
  885.                                     p.getInventory().addItem(giveItems.get(i));
  886.                                 }
  887.                             }
  888.                         }  
  889.                     }else{
  890.                         //full inv
  891.                         p.sendMessage(pre()+getConfig().getString("options.chat.full-inventory").replaceAll("&", "§"));
  892.                     }
  893.                 }
  894.                 p.updateInventory();
  895.             }
  896.         }else{
  897.             //idiot-proofing
  898.             if(p.getItemInHand().getAmount()!=1){
  899.                 p.sendMessage(pre()+getConfig().getString("options.chat.more-than-one-lootbox").replaceAll("&", "§"));
  900.             }else{
  901.                 p.sendMessage(pre()+getConfig().getString("options.chat.config-error").replaceAll("&", "§"));
  902.             }
  903.         }
  904.     }
  905.     private boolean isPermissible(Player p, String perm, boolean ret){
  906.         //permissions checker
  907.         if(p.hasPermission("lootbox."+perm.toLowerCase()) || p.isOp()){
  908.             return true;
  909.         }else if(ret){
  910.             p.sendMessage(pre()+getConfig().getString("options.chat.no-permissions").replaceAll("&", "§"));
  911.             return false;
  912.         }else{
  913.             return false;
  914.         }
  915.     }
  916.     private ArrayList<String> getConfigLore(String s, boolean rep){
  917.         ArrayList<String> lore = new ArrayList<String>();
  918.         if(s.contains("|")){
  919.             //split lore
  920.             String[] loreList = s.split("\\|");
  921.             for(int i=0;i<loreList.length;i++){
  922.                 if(rep){
  923.                     lore.add(loreList[i].replaceAll("&", "§").replaceAll("_", " "));
  924.                 }else{
  925.                     lore.add(loreList[i].replaceAll("&", "§"));
  926.                 }
  927.             }
  928.         }else{
  929.             //single line lore
  930.             if(rep){
  931.                 lore.add(s.replaceAll("&", "§").replaceAll("_", " "));
  932.             }else{
  933.                 lore.add(s.replaceAll("&", "§"));
  934.             }
  935.         }
  936.         return lore;
  937.     }
  938.     @SuppressWarnings("deprecation")
  939.     private ItemStack getConfigItemStack(String path){
  940.         //CAN RETURN NULL
  941.         //Construct them itemstack
  942.         String mat = getConfig().getString(path);
  943.         if(mat.contains(":")){
  944.             //if has a dmg value
  945.             String[] matList = mat.split(":");
  946.             if(matList.length!=2){
  947.                 return null;
  948.             }else{
  949.                 boolean go = true;
  950.                 int id1 = 54;
  951.                 int id2 = 0;
  952.                 //parse from string
  953.                 try{
  954.                     id1=Integer.parseInt(matList[0]);
  955.                 }catch(NumberFormatException e){
  956.                     e.printStackTrace();
  957.                     go = false;
  958.                 }
  959.                 try{
  960.                     id2=Integer.parseInt(matList[1]);
  961.                 }catch(NumberFormatException e){
  962.                     e.printStackTrace();
  963.                     go = false;
  964.                 }
  965.                 if(go){
  966.                     //construct item with dmg
  967.                     ItemStack item = new ItemStack(Material.getMaterial(id1));
  968.                     item.setDurability((short) id2);
  969.                     return item;
  970.                 }else{
  971.                     return null;
  972.                 }
  973.             }
  974.         }else{
  975.             //try parsing material id from string
  976.             int matId = 54;
  977.             boolean go = true;
  978.             try{
  979.                 matId = Integer.parseInt(mat);
  980.             }catch(NumberFormatException e){
  981.                 e.printStackTrace();
  982.                 go = false;
  983.             }
  984.             //construct
  985.             if(go){
  986.                 return new ItemStack(Material.getMaterial(matId));
  987.             }else{
  988.                 return null;
  989.             }
  990.         }
  991.     }
  992.     public String pre(){
  993.         return getConfig().getString("options.chat.prefix").replaceAll("&", "§");
  994.     }
  995.     public void execComm(Player p, ArrayList<String> eC){
  996.         for(int i6=0;i6<eC.size();i6++){
  997.             String str = eC.get(i6);
  998.             Boolean go3=true;
  999.             String[] cSplit = eC.get(i6).split(" ");
  1000.             for(int i7=0;i7<cSplit.length;i7++){
  1001.                 if(go3 && cSplit[i7].split(":").length==2){
  1002.                     go3 = false;
  1003.                     int chance2=0;
  1004.                     try{
  1005.                         chance2=Integer.parseInt(cSplit[i7].split(":")[1]);
  1006.                     }catch(NumberFormatException e){
  1007.                         log.info(pre()+"Part of an item's command's chance was not set correctly in the config, giving it a chance of 0.");
  1008.                     }
  1009.                     if(chance2!=0){
  1010.                         Random rand2 = new Random();
  1011.                         if(chance2 >= rand2.nextInt(100)+1){
  1012.                             for(int i8=0;i8<cSplit.length;i8++){
  1013.                                 if(i8!=i7){
  1014.                                     if(i8==0){
  1015.                                         str = cSplit[i8];
  1016.                                     }else{
  1017.                                         str = str + " " + cSplit[i8];
  1018.                                     }
  1019.                                 }
  1020.                             }
  1021.                             Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), str.replaceAll("\\{PLAYERNAME}", p.getName()));
  1022.                         }
  1023.                     }
  1024.                 }
  1025.             }
  1026.             if(go3){
  1027.                 Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), str.replaceAll("\\{PLAYERNAME}", p.getName()));
  1028.             }                      
  1029.         }
  1030.     }
  1031.     @SuppressWarnings({ "unchecked", "deprecation" })
  1032.     public ArrayList<ItemStack> getLootboxItems(String path){
  1033.         if(getConfig().getList("lootboxes."+path+".items")==null){
  1034.             return null;
  1035.         }else{
  1036.             ArrayList<ItemStack> items = new ArrayList<ItemStack>();
  1037.             ArrayList<String> list = (ArrayList<String>) getConfig().getList("lootboxes."+path+".items");
  1038.             for(int i=0;i<list.size();i++){
  1039.                 //Parse id,damage value, and amount from string
  1040.                 String[] split = list.get(i).split(" ");
  1041.                 ArrayList<String> lore = new ArrayList<String>();
  1042.                 LinkedList<Integer> emap = new LinkedList<Integer>();
  1043.                 LinkedList<Integer> amap = new LinkedList<Integer>();
  1044.                 boolean go = true;
  1045.                 int dmg=0;
  1046.                 int id = 0;
  1047.                 int amn=1;
  1048.                 String name = null;
  1049.                 if(split[0].contains(":")){
  1050.                     String[] doubleSplit = split[0].split(":");
  1051.                     try{
  1052.                         id=Integer.parseInt(doubleSplit[0]);
  1053.                         dmg=Integer.parseInt(doubleSplit[1]);
  1054.                     }catch(NumberFormatException e){
  1055.                         e.printStackTrace();
  1056.                         go = false;
  1057.                     }
  1058.                 }else{
  1059.                     try{
  1060.                         id=Integer.parseInt(split[0]);
  1061.                     }catch(NumberFormatException e){
  1062.                         e.printStackTrace();
  1063.                         go = false;
  1064.                     }
  1065.                 }
  1066.                 try{
  1067.                     amn=Integer.parseInt(split[1]);
  1068.                 }catch(NumberFormatException e){
  1069.                     e.printStackTrace();
  1070.                     go = false;
  1071.                 }
  1072.                 if(go){
  1073.                     for(int i2=2;i2<split.length;i2++){
  1074.                         //if parsed ok, get enchantments, lore, and name
  1075.                         String[] newSplit = split[i2].split(":", 2);
  1076.                         int ench = 0;
  1077.                         int eamn = 0;
  1078.                         if(newSplit[0].equalsIgnoreCase("name")){
  1079.                             //name
  1080.                             name = newSplit[1].replaceAll("&", "§").replaceAll("_", " ");
  1081.                         }else if(newSplit[0].equalsIgnoreCase("chance")){
  1082.                             //Do nothing
  1083.                         }else if(newSplit[0].equalsIgnoreCase("lore")){
  1084.                             //lore
  1085.                             lore = getConfigLore(newSplit[1], true);
  1086.                         }else{
  1087.                             //enchant and level
  1088.                             try{
  1089.                                 ench = Integer.parseInt(newSplit[0]);
  1090.                                 eamn = Integer.parseInt(newSplit[1]);
  1091.                             }catch(NumberFormatException e){
  1092.                                 e.printStackTrace();
  1093.                                 go = false;
  1094.                             }
  1095.                             if(go){
  1096.                                 emap.add(ench);
  1097.                                 amap.add(eamn);
  1098.                             }
  1099.                         }
  1100.                     }
  1101.                     //construct and add item
  1102.                     ItemStack is = new ItemStack(Material.getMaterial(id), amn);
  1103.                     ItemMeta im = is.getItemMeta();
  1104.                     for(int i3=0;i3<amap.size();i3++){
  1105.                         im.addEnchant(Enchantment.getById(emap.get(i3)), amap.get(i3), true);
  1106.                     }
  1107.                     im.setLore(lore);
  1108.                     if(name!=null){
  1109.                         im.setDisplayName(name);
  1110.                     }
  1111.                     is.setDurability((short) dmg);
  1112.                     is.setItemMeta(im);
  1113.                     items.add(is);
  1114.                 }
  1115.             }
  1116.             return items;
  1117.         }
  1118.     }
  1119.     @SuppressWarnings("unchecked")
  1120.     public ArrayList<Integer> getLootboxChance(String path){
  1121.         ArrayList<Integer> chances = new ArrayList<Integer>();
  1122.         ArrayList<String> list = (ArrayList<String>) getConfig().getList("lootboxes."+path+".items");
  1123.         for(int i=0;i<list.size();i++){
  1124.             //Parse id,damage value, and amount from string
  1125.             String[] split = list.get(i).split(" ");
  1126.             int chance = 100;
  1127.             for(int i2=2;i2<split.length;i2++){
  1128.                 String[] newSplit = split[i2].split(":", 2);
  1129.                 if(newSplit[0].equalsIgnoreCase("chance")){
  1130.                     //chance
  1131.                     try{
  1132.                         chance = Integer.parseInt(newSplit[1]);
  1133.                     }catch(NumberFormatException e){
  1134.                         log.info(pre()+"Part of an item's chance was not set correctly in the config, giving it a chance of 0.");
  1135.                         chance = 0;
  1136.                     }
  1137.                 }
  1138.             }
  1139.             chances.add(chance);
  1140.         }
  1141.         return chances;
  1142.     }
  1143.     public boolean isLootbox(ItemStack iih){
  1144.         if(iih!=null && new ItemStack(iih.getType()).equals(getConfigItemStack("options.items.lootbox-item"))){
  1145.             ItemMeta iihm = iih.getItemMeta();
  1146.             if(iihm.hasDisplayName() && iihm.hasLore()){
  1147.                 String sn = ChatColor.stripColor(iihm.getDisplayName()).replaceAll(" ", "_");
  1148.                 if(getConfig().contains("lootboxes."+sn)){
  1149.                     String rn = sn.replaceAll("_", " ");
  1150.                     rn = getConfig().getString("lootboxes."+sn+".color").replaceAll("&", "§") + rn;
  1151.                     ArrayList<String> testLore = (ArrayList<String>) iihm.getLore();
  1152.                     ArrayList<String> configLore = getConfigLore(getConfig().getString("lootboxes."+sn+".lore"), false);
  1153.                     if(testLore.equals(configLore) && rn.equalsIgnoreCase(iihm.getDisplayName())){
  1154.                         return true;
  1155.                     }else{
  1156.                         return false;
  1157.                     }
  1158.                 }else{
  1159.                     return false;
  1160.                 }
  1161.             }else{
  1162.                 return false;
  1163.             }
  1164.         }else{
  1165.             return false;
  1166.         }
  1167.     }
  1168.     public boolean isKey(ItemStack iih){
  1169.         if(iih!=null && new ItemStack(iih.getType()).equals(getConfigItemStack("options.items.key-item"))){
  1170.             ItemMeta iihm = iih.getItemMeta();
  1171.             if(iihm.hasDisplayName() && iihm.hasLore()){
  1172.                 String sn = ChatColor.stripColor(iihm.getDisplayName()).replaceAll(" ", "_");
  1173.                 if(getConfig().contains("keys."+sn)){
  1174.                     String rn = sn.replaceAll("_", " ");
  1175.                     rn = getConfig().getString("keys."+sn+".color").replaceAll("&", "§") + rn;
  1176.                     ArrayList<String> testLore = (ArrayList<String>) iihm.getLore();
  1177.                     ArrayList<String> configLore = getConfigLore(getConfig().getString("keys."+sn+".lore"), false);
  1178.                     if(testLore.equals(configLore) && rn.equalsIgnoreCase(iihm.getDisplayName())){
  1179.                         return true;
  1180.                     }else{
  1181.                         return false;
  1182.                     }
  1183.                 }else{
  1184.                     return false;
  1185.                 }
  1186.             }else{
  1187.                 return false;
  1188.             }
  1189.         }else{
  1190.             return false;
  1191.         }
  1192.     }
  1193.     public boolean canFitItems(Inventory inv, ArrayList<ItemStack> items){
  1194.         //TODO Remove lootbox and key from list
  1195.         Inventory copy = Bukkit.getServer().createInventory(null, 36);
  1196.         for(int i=0;i<36;i++){
  1197.             if(inv.getItem(i)!=null){
  1198.                 copy.addItem(inv.getItem(i));
  1199.             }
  1200.         }
  1201.         boolean can = true;
  1202.         for(int i=0;i<items.size();i++){
  1203.             if(!copy.addItem(items.get(i)).isEmpty()){
  1204.                 can = false;
  1205.             }  
  1206.         }
  1207.         copy.clear();
  1208.         return can;
  1209.     }
  1210.     public ArrayList<ItemStack> getItemsToGive(int min, int max, ArrayList<ItemStack> mustItems, ArrayList<ItemStack> otherItems, ArrayList<ItemStack> leastItems){
  1211.         ArrayList<ItemStack> returnList = new ArrayList<ItemStack>();
  1212.         if(mustItems.size()<min){
  1213.             returnList.addAll(mustItems);
  1214.             if(otherItems.size()+mustItems.size()+leastItems.size()<min){
  1215.                 returnList.addAll(otherItems);
  1216.                 returnList.addAll(leastItems);
  1217.             }else if(otherItems.size()+mustItems.size()<min){
  1218.                 returnList.addAll(otherItems);
  1219.                 for(int i=0;i<min-(mustItems.size()+otherItems.size());i++){
  1220.                     Random rand = new Random();
  1221.                     int num = rand.nextInt(leastItems.size());
  1222.                     returnList.add(leastItems.get(num));
  1223.                     leastItems.remove(num);
  1224.                 }
  1225.             }else{
  1226.                 for(int i=0;i<min-mustItems.size();i++){
  1227.                     Random rand = new Random();
  1228.                     int num = rand.nextInt(otherItems.size());
  1229.                     returnList.add(otherItems.get(num));
  1230.                     otherItems.remove(num);
  1231.                 }
  1232.             }
  1233.             return returnList;
  1234.         }else if(mustItems.size()>max){
  1235.             for(int i=0;i<max;i++){
  1236.                 Random rand = new Random();
  1237.                 int num = rand.nextInt(mustItems.size());
  1238.                 returnList.add(mustItems.get(num));
  1239.                 mustItems.remove(num);
  1240.             }
  1241.             return returnList;
  1242.         }else if(mustItems.size()+otherItems.size()<max){
  1243.             returnList.addAll(mustItems);
  1244.             returnList.addAll(otherItems);
  1245.             return returnList;
  1246.         }else{
  1247.             return mustItems;
  1248.         }
  1249.     }
  1250.     public int getMaxItems(String path){
  1251.         boolean go = true;
  1252.         int max = 0;
  1253.         if(getConfig().get("lootboxes."+path+".max-items")==null){
  1254.             max = 100000;
  1255.         }else if(getConfig().get("lootboxes."+path+".max-items") instanceof Integer){
  1256.             max = getConfig().getInt("lootboxes."+path+".max-items");
  1257.         }else if(getConfig().get("lootboxes."+path+".max-items") instanceof String){
  1258.             try{
  1259.                 max = Integer.parseInt(getConfig().getString("lootboxes."+path+".max-items"));
  1260.             }catch(NumberFormatException e){
  1261.                 go = false;
  1262.             }
  1263.         }else{
  1264.             go = false;
  1265.         }
  1266.         if(go){
  1267.             return max;
  1268.         }else{
  1269.             return -1;
  1270.         }
  1271.     }
  1272.     public int getMinItems(String path){
  1273.         boolean go = true;
  1274.         int min = 0;
  1275.         if(getConfig().get("lootboxes."+path+".min-items")==null){
  1276.             min = 0;
  1277.         }else if(getConfig().get("lootboxes."+path+".min-items") instanceof Integer){
  1278.             min = getConfig().getInt("lootboxes."+path+".min-items");
  1279.         }else if(getConfig().get("lootboxes."+path+".min-items") instanceof String){
  1280.             try{
  1281.                 min = Integer.parseInt(getConfig().getString("lootboxes."+path+".min-items"));
  1282.             }catch(NumberFormatException e){
  1283.                 go = false;
  1284.             }
  1285.         }else{
  1286.             go = false;
  1287.         }
  1288.         if(go){
  1289.             return min;
  1290.         }else{
  1291.             return -1;
  1292.         }
  1293.     }
  1294.     public boolean hasItemInHand(ItemStack iih){
  1295.         if(!iih.equals(null) && !iih.getType().equals(Material.AIR)){
  1296.             return true;
  1297.         }else{
  1298.             return false;
  1299.         }
  1300.     }
  1301. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement