Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.TheOatBaron.Bukkit;
- import java.io.File;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.LinkedList;
- import java.util.Map.Entry;
- import java.util.Random;
- import java.util.logging.Logger;
- import org.bukkit.Bukkit;
- import org.bukkit.ChatColor;
- import org.bukkit.Material;
- import org.bukkit.Sound;
- import org.bukkit.command.Command;
- import org.bukkit.command.CommandSender;
- import org.bukkit.enchantments.Enchantment;
- import org.bukkit.entity.Player;
- import org.bukkit.event.EventHandler;
- import org.bukkit.event.Listener;
- import org.bukkit.event.block.Action;
- import org.bukkit.event.inventory.InventoryClickEvent;
- import org.bukkit.event.player.PlayerInteractEvent;
- import org.bukkit.inventory.Inventory;
- import org.bukkit.inventory.ItemStack;
- import org.bukkit.inventory.meta.ItemMeta;
- import org.bukkit.plugin.java.JavaPlugin;
- //
- //As a note to the Bukkit Staff:
- //
- //I'm sorry if this plugin causes anyone to be diagnosed with PTSD after going through it.
- //
- public class Lootbox extends JavaPlugin implements Listener{
- Logger log = Bukkit.getServer().getLogger();
- Random rand;
- @Override
- public void onEnable(){
- getServer().getPluginManager().registerEvents(this, this);
- if (!getDataFolder().exists()){
- getDataFolder().mkdir();
- }
- File file = new File(getDataFolder(), "config.yml");
- if(!file.exists()){
- this.saveDefaultConfig();
- }
- if(!getConfig().contains("options")){
- getConfig().set("options.chat.prefix", "&7[&6Lootbox&7] ");
- getConfig().set("options.chat.no-permissions", "&7You do not have permission to do this!");
- getConfig().set("options.chat.improper-usage-of-command", "&7Improper usage of the command!");
- getConfig().set("options.chat.empty-lootbox", "&7The lootbox appears to be empty...");
- getConfig().set("options.chat.more-than-one-lootbox", "&7You can only have ONE lootbox in your hand while opening it!");
- getConfig().set("options.chat.more-than-one-key", "&7You can only have ONE key in your hand while using it!");
- getConfig().set("options.chat.full-inventory", "&7Your inventory is too full for this!");
- getConfig().set("options.chat.config-error", "&7Something went wrong in the Lootbox config.yml :C");
- getConfig().set("options.chat.reload", "&7Lootbox config.yml has been reloaded!");
- getConfig().set("options.chat.given-lootbox", "&7You have been given a lootbox!");
- getConfig().set("options.chat.gave-lootbox", "&7You have gave a lootbox!");
- getConfig().set("options.chat.given-key", "&7You have been given a key!");
- getConfig().set("options.chat.gave-key", "&7You have gave a key!");
- getConfig().set("options.chat.lootbox-given", "&7You have been given a lootbox!");
- getConfig().set("options.chat.no-key", "&7This box requires a key to open! If you have the proper one, right click with it!");
- getConfig().set("options.chat.nothing", "&7This key does not open anything!");
- getConfig().set("options.chat.no-lootbox-to-open", "&7This key can't open any lootboxes you have, make sure they aren't stacked!");
- getConfig().set("options.chat.no-exist", "&7That lootbox/key doesn't exist! Remember, names are case-sensitive!");
- getConfig().set("options.chat.offline-player", "&7That player is offline!");
- getConfig().set("options.items.lootbox-item", "54");
- getConfig().set("options.items.key-item", "131");
- getConfig().set("options.other.make-noise-when-lootbox-opens", true);
- }
- //v1.2 Support
- if(!getConfig().contains("options.chat.no-key-needed")){
- getConfig().set("options.chat.seperator", "&7----------------");
- getConfig().set("options.chat.lootboxes", "&8Lootboxes");
- getConfig().set("options.chat.keys", "&8Keys");
- getConfig().set("options.chat.improper-item", "&7You aren't holding the proper item!");
- getConfig().set("options.chat.no-key-needed", "&8No key needed to open this lootbox!");
- getConfig().set("options.chat.no-lootboxes", "&8This key doesn't open any lootbox!");
- }
- //v1.3 Support
- if(!getConfig().contains("options.chat.lootbox-too-big")){
- getConfig().set("options.chat.not-a-lootbox", "&7You are not holding a lootbox!");
- getConfig().set("options.chat.not-a-key", "&7You are not holding a key!");
- getConfig().set("options.chat.lootbox-too-big", "&7This lootbox contains too many items to open! Contact an administrator!");
- }
- //v1.4 Support
- if(!getConfig().contains("options.chat.lootbox-created")){
- getConfig().set("options.chat.editor-name-complete", "&7A name has been successfully added to the item!");
- getConfig().set("options.chat.editor-lore-complete", "&7A line of lore has been successfully added to the item!");
- getConfig().set("options.chat.editor-chance-complete", "&7A chance has been successfully added to the item!");
- getConfig().set("options.chat.no-item", "&7You must be holding an item!");
- getConfig().set("options.chat.empty-inventory", "&7You must have something in your inventory to do this! Lootbox not created.");
- getConfig().set("options.chat.already-exists", "&7That lootbox/key already exists! Lootbox not created.");
- getConfig().set("options.chat.permission-in-use", "&7That permission is already in use! Lootbox not created.");
- 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.");
- getConfig().set("options.chat.lootbox-created", "&7You have created a lootbox!");
- }
- if(!getConfig().contains("lootboxes")){
- getConfig().createSection("lootboxes");
- }
- if(!getConfig().contains("keys")){
- getConfig().createSection("keys");
- }
- getConfig().options().header("Lootbox v1.4 (TheOatBaron) - http://dev.bukkit.org/bukkit-plugins/lootbox/");
- getConfig().options().copyHeader();
- getConfig().options().copyDefaults(true);
- saveConfig();
- reloadConfig();
- }
- @SuppressWarnings({ "deprecation", "unchecked" })
- @Override
- public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
- if(label.equalsIgnoreCase("lootbox")){
- if(sender instanceof Player){
- Player initial = (Player) sender;
- if(args.length==0 || args[0].equalsIgnoreCase("help")){
- if(isPermissible(initial, "help", true)){
- initial.sendMessage(pre()+"§7Lootbox Help");
- initial.sendMessage(pre()+"§8----------------------");
- 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!");
- initial.sendMessage(pre()+"§7/lootbox help §0- §8Displays this help page.");
- if(isPermissible(initial, "preview", false)){
- initial.sendMessage(pre()+"§7/lootbox preview §0- §8Shows what you can get in a lootbox, excluding commands. (i.e. Money)");
- }
- if(isPermissible(initial, "keys", false)){
- initial.sendMessage(pre()+"§7/lootbox keys §0- §8Shows what keys can be used to open the lootbox in hand.");
- }
- if(isPermissible(initial, "lootboxes", false)){
- initial.sendMessage(pre()+"§7/lootbox lootboxes §0- §8Shows what lootboxes can be opened with the key in hand.");
- }
- if(isPermissible(initial, "info", false)){
- initial.sendMessage(pre()+"§7/lootbox info §0- §8Get information on an item.");
- }
- if(isPermissible(initial, "admin.reload", false)){
- initial.sendMessage(pre()+"§7/lootbox reload §0- §8Reloads the config.yml, save first.");
- }
- if(isPermissible(initial, "admin.list", false)){
- initial.sendMessage(pre()+"§7/lootbox list <permissions/lootboxes/keys> §0- §8Lists the commands/lootboxes/keys and their permission nodes.");
- }
- if(isPermissible(initial, "admin.editor", false)){
- initial.sendMessage(pre()+"§7/lootbox editor <name/lore/chance> <args> §0- §8Give an item a name/lore/chance.");
- }
- if(isPermissible(initial, "admin.give", false)){
- initial.sendMessage(pre()+"§7/lootbox give <username> <lootbox:key> <lootbox/key name> §0- §8Gives a player a lootbox/key.");
- }
- if(isPermissible(initial, "admin.create", false)){
- 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.");
- }
- }
- }else if(args[0].equalsIgnoreCase("give") && args.length > 3){
- if(isPermissible(initial, "admin.give", true)){
- if(Bukkit.getServer().getPlayer(args[1])==null){
- initial.sendMessage(pre()+getConfig().getString("options.chat.offline-player").replaceAll("&", "§"));
- log.info(pre() + initial.getName() + " tried to give " + args[1] + " a lootbox/key, but they were offline!");
- }else{
- Player target = Bukkit.getServer().getPlayer(args[1]);
- ItemStack is = getConfigItemStack("options.items.lootbox-item");
- ItemStack is2 = getConfigItemStack("options.items.key-item");
- if(is!=null && is2!=null){
- String dispName = "";
- for(int i=3;i<args.length;i++){
- if(i==3){
- dispName = dispName + args[i];
- }else{
- dispName = dispName + " " + args[i];
- }
- }
- dispName = ChatColor.stripColor(dispName.replaceAll(" ", "_").replaceAll("&", "§"));
- if(args[2].equalsIgnoreCase("lootbox") || args[2].equalsIgnoreCase("box") || args[2].equalsIgnoreCase("chest")){
- if(getConfig().contains("lootboxes."+dispName)){
- ItemMeta im = is.getItemMeta();
- im.setDisplayName(getConfig().getString("lootboxes."+dispName+".color").replaceAll("&", "§") + dispName.replaceAll("_", " "));
- im.setLore(getConfigLore(getConfig().getString("lootboxes."+dispName+".lore"), false));
- is.setItemMeta(im);
- initial.sendMessage(pre()+getConfig().getString("options.chat.gave-lootbox").replaceAll("&", "§"));
- target.sendMessage(pre()+getConfig().getString("options.chat.given-lootbox").replaceAll("&", "§"));
- if(target.getInventory().firstEmpty()==-1){
- target.sendMessage(pre()+getConfig().getString("options.chat.full-inventory").replaceAll("&", "§"));
- Bukkit.getServer().getWorld(target.getWorld().getName()).dropItem(target.getLocation(), is);
- log.info(pre() + initial.getName() + " has given " + target.getName() + " a lootbox, but they had no room so the item dropped at their feet.");
- }else{
- target.getInventory().addItem(is);
- log.info(pre() + initial.getName() + " has given " + target.getName() + " a lootbox.");
- }
- }else{
- initial.sendMessage(pre()+getConfig().getString("options.chat.no-exist").replaceAll("&", "§"));
- }
- }else if(args[2].equalsIgnoreCase("key")){
- if(getConfig().contains("keys."+dispName)){
- ItemMeta im = is2.getItemMeta();
- im.setDisplayName(getConfig().getString("keys."+dispName+".color").replaceAll("&", "§") + dispName.replaceAll("_", " "));
- im.setLore(getConfigLore(getConfig().getString("keys."+dispName+".lore"), false));
- is2.setItemMeta(im);
- initial.sendMessage(pre()+getConfig().getString("options.chat.gave-key").replaceAll("&", "§"));
- target.sendMessage(pre()+getConfig().getString("options.chat.given-key").replaceAll("&", "§"));
- if(target.getInventory().firstEmpty()==-1){
- target.sendMessage(pre()+getConfig().getString("options.chat.full-inventory").replaceAll("&", "§"));
- Bukkit.getServer().getWorld(target.getWorld().getName()).dropItem(target.getLocation(), is2);
- log.info(pre() + initial.getName() + " has given " + target.getName() + " a key, but they had no room so the item dropped at their feet.");
- }else{
- target.getInventory().addItem(is2);
- log.info(pre() + initial.getName() + " has given " + target.getName() + " a key.");
- }
- }else{
- initial.sendMessage(pre()+getConfig().getString("options.chat.no-exist").replaceAll("&", "§"));
- }
- }else{
- initial.sendMessage(pre()+getConfig().getString("options.chat.improper-usage-of-command").replaceAll("&", "§"));
- }
- }else{
- initial.sendMessage(pre()+getConfig().getString("options.chat.config-error").replaceAll("&", "§"));
- }
- }
- }
- }else if(args[0].equalsIgnoreCase("reload") || args[0].equalsIgnoreCase("save")){
- if(isPermissible(initial, "admin.reload", true)){
- reloadConfig();
- initial.sendMessage(pre()+getConfig().getString("options.chat.reload").replaceAll("&", "§"));
- }
- }else if(args[0].equalsIgnoreCase("keys") || args[0].equalsIgnoreCase("key")){
- if(isPermissible(initial, "keys", true)){
- ItemStack iih = initial.getItemInHand();
- if(isLootbox(iih)){
- ItemMeta iihm = iih.getItemMeta();
- String sn = ChatColor.stripColor(iihm.getDisplayName()).replaceAll(" ", "_");
- String rn = sn.replaceAll("_", " ");
- rn = getConfig().getString("lootboxes."+sn+".color").replaceAll("&", "§") + rn;
- initial.sendMessage(pre()+getConfig().getString("options.chat.keys").replaceAll("&", "§"));
- initial.sendMessage(pre()+getConfig().getString("options.chat.seperator").replaceAll("&", "§"));
- boolean nnk = true;
- for(String key : getConfig().getConfigurationSection("keys").getKeys(false)){
- if(getConfig().getList("keys."+key+".opens-lootboxes").contains(sn)){
- initial.sendMessage(pre()+(getConfig().getString("keys."+key+".color")+key).replaceAll("&", "§").replaceAll("_", " "));
- nnk = false;
- }
- }
- if(nnk){
- initial.sendMessage(pre()+getConfig().getString("options.chat.no-key-needed").replaceAll("&", "§"));
- }
- }else{
- initial.sendMessage(pre()+getConfig().getString("options.chat.not-a-lootbox").replaceAll("&", "§"));
- }
- }
- }else if(args[0].equalsIgnoreCase("lootboxes") || args[0].equalsIgnoreCase("lootboxs") || args[0].equalsIgnoreCase("lootbox")){
- if(isPermissible(initial, "lootboxes", true)){
- ItemStack iih = initial.getItemInHand();
- if(isKey(iih)){
- ItemMeta iihm = iih.getItemMeta();
- String sn = ChatColor.stripColor(iihm.getDisplayName()).replaceAll(" ", "_");
- String rn = sn.replaceAll("_", " ");
- rn = getConfig().getString("keys."+sn+".color").replaceAll("&", "§") + rn;
- initial.sendMessage(pre()+getConfig().getString("options.chat.lootboxes").replaceAll("&", "§"));
- initial.sendMessage(pre()+getConfig().getString("options.chat.seperator").replaceAll("&", "§"));
- ArrayList<String> list = (ArrayList<String>) getConfig().getList("keys."+sn+".opens-lootboxes");
- boolean nnk=true;
- for(int i=0;i<list.size();i++){
- if(getConfig().contains("lootboxes."+list.get(i))){
- initial.sendMessage(pre()+(getConfig().getString("lootboxes."+list.get(i)+".color")+list.get(i)).replaceAll("&", "§").replaceAll("_", " "));
- nnk = false;
- }else{
- log.warning(pre()+"Lootbox "+list.get(i)+" doesn't exist!");
- }
- }
- if(nnk){
- initial.sendMessage(pre()+getConfig().getString("options.chat.no-lootboxes").replaceAll("&", "§"));
- }
- }else{
- initial.sendMessage(pre()+getConfig().getString("options.chat.not-a-key").replaceAll("&", "§"));
- }
- }
- }else if(args[0].equalsIgnoreCase("editor")){
- if(isPermissible(initial, "admin.editor", true)){
- ItemStack iih = initial.getItemInHand();
- if(hasItemInHand(iih)){
- if(args.length>2 && args[1].equalsIgnoreCase("name")){
- String name = args[2];
- if(!name.startsWith("&")){
- name="&7"+name;
- }
- for(int i=3;i<args.length;i++){
- name = name+" "+args[i];
- }
- name = name.replaceAll("&", "§");
- ItemMeta im = iih.getItemMeta();
- im.setDisplayName(name);
- iih.setItemMeta(im);
- initial.sendMessage(pre()+getConfig().getString("options.chat.editor-name-complete").replaceAll("&", "§"));
- }else if(args.length>2 && args[1].equalsIgnoreCase("lore")){
- ItemMeta im = iih.getItemMeta();
- String name = args[2];
- for(int i=3;i<args.length;i++){
- name = name+" "+args[i];
- }
- name = name.replaceAll("&", "§");
- ArrayList<String> lore = new ArrayList<String>();
- if(im.hasLore()){
- lore = (ArrayList<String>) im.getLore();
- }
- lore.add(name);
- im.setLore(lore);
- iih.setItemMeta(im);
- initial.sendMessage(pre()+getConfig().getString("options.chat.editor-lore-complete").replaceAll("&", "§"));
- }else if(args.length==3 && args[1].equalsIgnoreCase("chance")){
- ItemMeta im = iih.getItemMeta();
- boolean go=true;
- int chance = 100;
- try{
- chance = Integer.parseInt(args[2]);
- }catch(NumberFormatException e){
- go = false;
- }
- if(go && chance>-1 && chance<101){
- ArrayList<String> lore = new ArrayList<String>();
- if(im.hasLore()){
- lore = (ArrayList<String>) im.getLore();
- }
- lore.add("§7"+chance+"% Chance");
- im.setLore(lore);
- iih.setItemMeta(im);
- initial.sendMessage(pre()+getConfig().getString("options.chat.editor-chance-complete").replaceAll("&", "§"));
- }else{
- initial.sendMessage(pre()+getConfig().getString("options.chat.improper-usage-of-command").replaceAll("&", "§"));
- }
- }else{
- initial.sendMessage(pre()+getConfig().getString("options.chat.improper-usage-of-command").replaceAll("&", "§"));
- }
- }else{
- initial.sendMessage(pre()+getConfig().getString("options.chat.no-item").replaceAll("&", "§"));
- }
- }
- }else if(args[0].equalsIgnoreCase("list")){
- if(isPermissible(initial, "admin.list", true)){
- if(args.length==2){
- if(args[1].equalsIgnoreCase("permissions")){
- initial.sendMessage(pre()+"§7Permission Nodes:");
- initial.sendMessage(pre()+"§8 - /lootbox help - lootbox.help");
- initial.sendMessage(pre()+"§8 - /lootbox preview - lootbox.preview");
- initial.sendMessage(pre()+"§8 - /lootbox keys - lootbox.keys");
- initial.sendMessage(pre()+"§8 - /lootbox lootboxes - lootbox.lootboxes");
- initial.sendMessage(pre()+"§8 - /lootbox info - lootbox.info");
- initial.sendMessage(pre()+"§8 - /lootbox editor <name/lore/chance> <args> - lootbox.admin.editor");
- initial.sendMessage(pre()+"§8 - /lootbox list <permissions/keys/lootboxes> - lootbox.admin.list");
- initial.sendMessage(pre()+"§8 - /lootbox create [nokey] - lootbox.admin.create");
- initial.sendMessage(pre()+"§8 - /lootbox reload - lootbox.admin.reload");
- initial.sendMessage(pre()+"§8 - /lootbox give - lootbox.admin.give");
- initial.sendMessage(pre()+"§7Note: This does not include usage permissions, please see the following for usage permissions:");
- initial.sendMessage(pre()+"§8 - /lootbox list keys");
- initial.sendMessage(pre()+"§8 - /lootbox list lootboxes");
- }else if(args[1].equalsIgnoreCase("keys") || args[1].equalsIgnoreCase("key")){
- initial.sendMessage(pre()+"§7Keys - Permission Nodes");
- for(String key : getConfig().getConfigurationSection("keys").getKeys(false)){
- initial.sendMessage(pre()+getConfig().getString("keys."+key+".color").replaceAll("&", "§")+key.replaceAll("_", " ")+" §8- §7" + "lootbox.use.key."+getConfig().getString("keys."+key+".permission"));
- }
- }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")){
- initial.sendMessage(pre()+"§7Lootboxes - Permission Nodes");
- for(String lootbox : getConfig().getConfigurationSection("lootboxes").getKeys(false)){
- initial.sendMessage(pre()+getConfig().getString("lootboxes."+lootbox+".color").replaceAll("&", "§")+lootbox.replaceAll("_", " ")+" §8- §7" + "lootbox.use.lootbox."+getConfig().getString("lootboxes."+lootbox+".permission"));
- }
- }else{
- initial.sendMessage(pre()+getConfig().getString("options.chat.improper-usage-of-command").replaceAll("&", "§"));
- }
- }else{
- initial.sendMessage(pre()+getConfig().getString("options.chat.improper-usage-of-command").replaceAll("&", "§"));
- }
- }
- }else if(args[0].equalsIgnoreCase("info")){
- if(isPermissible(initial, "info", true)){
- ItemStack iih = initial.getItemInHand();
- if(hasItemInHand(iih)){
- ItemMeta im = iih.getItemMeta();
- initial.sendMessage("§7Lootbox Info");
- initial.sendMessage("§8-----------------");
- if(iih.getDurability()==0){
- initial.sendMessage("§8ID: §7" + iih.getTypeId());
- }else{
- initial.sendMessage("§8ID: §7" + iih.getTypeId()+":"+iih.getDurability());
- }
- if(im.hasDisplayName()){
- initial.sendMessage("§8Name: §7" + im.getDisplayName());
- }
- if(im.hasLore()){
- initial.sendMessage("§8Lore: ");
- for(int i=0;i<im.getLore().size();i++){
- initial.sendMessage(" §7- "+"§5§o"+im.getLore().get(i));
- }
- }
- if(im.hasEnchants()){
- initial.sendMessage("§8Enchantments: ");
- Iterator<Entry<Enchantment, Integer>> it = iih.getEnchantments().entrySet().iterator();
- while (it.hasNext()) {
- Entry<Enchantment, Integer> pairs = it.next();
- Enchantment ench = pairs.getKey();
- initial.sendMessage("§7"+ench.getName()+" ("+ench.getId()+") - " + pairs.getValue() + " Levels");
- }
- }
- }else{
- initial.sendMessage(pre()+getConfig().getString("options.chat.no-item").replaceAll("&", "§"));
- }
- }
- }else if(args[0].equalsIgnoreCase("create")){
- boolean reqkey=true;
- if(args.length==2 && args[1].equalsIgnoreCase("nokey")){
- reqkey=false;
- }
- if(isPermissible(initial, "admin.create", true)){
- Inventory iinv = initial.getInventory();
- ItemStack[] list = iinv.getContents();
- if(list.length!=0){
- int fid = 0;
- int sid = 0;
- boolean go = true;
- try{
- fid = Integer.parseInt(getConfig().getString("options.items.lootbox-item"));
- sid = Integer.parseInt(getConfig().getString("options.items.key-item"));
- }catch(NumberFormatException e){
- go = false;
- }
- if(go){
- boolean quickpass = false;
- if(reqkey){
- 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()){
- quickpass = true;
- }
- }else if(list[0]!=null&& list[0].getTypeId()==fid&& list[0].hasItemMeta() && list[0].getItemMeta().hasLore() && list[0].getItemMeta().hasDisplayName()){
- quickpass = true;
- }
- if(quickpass){
- ArrayList<String> flore = (ArrayList<String>) list[0].getItemMeta().getLore();
- ArrayList<String> slore = new ArrayList<String>();
- String sname = "";
- String suname = "";
- if(reqkey){
- slore = (ArrayList<String>) list[1].getItemMeta().getLore();
- }
- String fname = list[0].getItemMeta().getDisplayName();
- if(reqkey){
- sname = list[1].getItemMeta().getDisplayName();
- }
- String funame = ChatColor.stripColor(list[0].getItemMeta().getDisplayName().replaceAll(" ", "_"));
- if(reqkey){
- suname = ChatColor.stripColor(list[1].getItemMeta().getDisplayName().replaceAll(" ", "_"));
- }
- if(getConfig().contains("lootboxes."+funame) || (reqkey && getConfig().contains("keys."+suname))){
- initial.sendMessage(pre()+getConfig().getString("options.chat.already-exists").replaceAll("&", "§"));
- }else{
- String fpath = "lootboxes."+funame;
- String spath = "";
- if(reqkey){
- spath = "keys."+suname;
- }
- getConfig().createSection(fpath);
- if(reqkey){
- getConfig().createSection(spath);
- }
- String fcolor = "&f&o";
- String scolor = "&f&o";
- if(fname.startsWith("§")){
- fcolor = "&"+fname.charAt(1);
- }
- if(reqkey && sname.startsWith("§")){
- scolor = "&"+sname.charAt(1);
- }
- getConfig().set(fpath+".color", fcolor);
- if(reqkey){
- getConfig().set(spath+".color", scolor);
- }
- String fstr = flore.get(0).replaceAll("§","&");
- String sstr = "";
- if(reqkey){
- sstr = slore.get(0).replaceAll("§","&");
- }
- for(int i=1;i<flore.size();i++){
- fstr = fstr+"|"+flore.get(i).replaceAll("§","&");
- }
- getConfig().set(fpath+".lore", fstr);
- if(reqkey){
- for(int i=1;i<slore.size();i++){
- sstr = sstr+"|"+slore.get(i).replaceAll("§","&");
- }
- }
- if(reqkey){
- getConfig().set(spath+".lore", sstr);
- }
- boolean sgo=true;
- for(String box : getConfig().getConfigurationSection("lootboxes").getKeys(false)){
- if(!box.equals(funame) && getConfig().getString("lootboxes."+box+".permission").equalsIgnoreCase(funame)){
- sgo = false;
- }
- }
- if(reqkey){
- for(String key : getConfig().getConfigurationSection("keys").getKeys(false)){
- if(!key.equals(suname) && getConfig().getString("keys."+key+".permission").equalsIgnoreCase(suname)){
- sgo = false;
- }
- }
- }
- if(sgo){
- getConfig().set(fpath+".permission", funame.toLowerCase());
- if(reqkey){
- getConfig().set(fpath+".requires-a-key", true);
- getConfig().set(spath+".permission", suname.toLowerCase());
- }else{
- getConfig().set(fpath+".requires-a-key", false);
- }
- ArrayList<String> newList = new ArrayList<String>();
- newList.add(funame);
- if(reqkey){
- getConfig().set(spath+".opens-lootboxes", newList);
- }
- initial.sendMessage(pre()+getConfig().getString("options.chat.lootbox-created").replaceAll("&", "§"));
- ArrayList<String> allItems = new ArrayList<String>();
- if(reqkey){
- for(int i=2;i<iinv.getSize();i++){
- ItemStack item = iinv.getItem(i);
- if(item!=null && item.getType()!=Material.AIR){
- allItems.add(toLootboxString(item));
- }
- }
- }else{
- for(int i=1;i<iinv.getSize();i++){
- ItemStack item = iinv.getItem(i);
- if(item!=null && item.getType()!=Material.AIR){
- allItems.add(toLootboxString(item));
- }
- }
- }
- getConfig().set(fpath+".items", allItems);
- saveConfig();
- reloadConfig();
- }else{
- initial.sendMessage(pre()+getConfig().getString("options.chat.how-to-create").replaceAll("&", "§"));
- getConfig().set(fpath, null);
- if(reqkey){
- getConfig().set(spath, null);
- }
- }
- }
- }else{
- initial.sendMessage(pre()+getConfig().getString("options.chat.how-to-create").replaceAll("&", "§"));
- }
- }else{
- initial.sendMessage(pre()+getConfig().getString("options.chat.config-error").replaceAll("&", "§"));
- }
- }else{
- initial.sendMessage(pre()+getConfig().getString("options.chat.empty-inventory").replaceAll("&", "§"));
- }
- }
- }else if(args[0].equalsIgnoreCase("preview")){
- if(isPermissible(initial, "preview", true)){
- ItemStack iih = initial.getItemInHand();
- if(isLootbox(iih)){
- ItemMeta iihm = iih.getItemMeta();
- String sn = ChatColor.stripColor(iihm.getDisplayName()).replaceAll(" ", "_");
- String rn = sn.replaceAll("_", " ");
- rn = getConfig().getString("lootboxes."+sn+".color").replaceAll("&", "§") + rn;
- if(isLootbox(iih)){
- ArrayList<String> list = (ArrayList<String>) getConfig().getList("lootboxes."+sn+".items");
- if(list.isEmpty()){
- initial.sendMessage(pre()+getConfig().getString("options.chat.empty-lootbox").replaceAll("&", "§"));
- }else{
- String path = ChatColor.stripColor(iih.getItemMeta().getDisplayName().replaceAll(" ", "_"));
- ArrayList<ItemStack> items = getLootboxItems(path);
- Inventory prev = Bukkit.getServer().createInventory(null, 45, "Lootbox Preview");
- if(canFitItems(prev, items)){
- for(int i10=0;i10<9;i10++){
- prev.setItem(i10, iih);
- }
- ArrayList<Integer> chances = getLootboxChance(path);
- for(int i=0;i<items.size();i++){
- ItemStack is = items.get(i);
- if(is.hasItemMeta() && is.getItemMeta().hasLore()){
- ItemMeta im = is.getItemMeta();
- ArrayList<String> lore = (ArrayList<String>) im.getLore();
- lore.add("§7"+chances.get(i)+"% Chance");
- im.setLore(lore);
- is.setItemMeta(im);
- if(chances.get(i)!=0){
- prev.addItem(is);
- }
- }else{
- if(!is.hasItemMeta()){
- is.setItemMeta(null);
- }
- ItemMeta im = is.getItemMeta();
- ArrayList<String> lore = new ArrayList<String>();
- lore.add("§7"+chances.get(i)+"% Chance");
- im.setLore(lore);
- is.setItemMeta(im);
- if(chances.get(i)!=0){
- prev.addItem(is);
- }
- }
- }
- initial.openInventory(prev);
- }else{
- initial.sendMessage(pre()+getConfig().getString("options.chat.lootbox-too-big").replaceAll("&", "§"));
- }
- }
- }
- }else{
- initial.sendMessage(pre()+getConfig().getString("options.chat.not-a-lootbox").replaceAll("&", "§"));
- }
- }
- }else{
- initial.sendMessage(pre()+getConfig().getString("options.chat.improper-usage-of-command").replaceAll("&", "§"));
- }
- }else{
- if(args.length==1 && args[0].equalsIgnoreCase("reload")){
- reloadConfig();
- log.info(pre() + "config.yml reloaded!");
- }else if(args[0].equalsIgnoreCase("give") && args.length > 3){
- if(Bukkit.getServer().getPlayer(args[1])==null){
- log.info(pre() + "Server console tried to give " + args[1] + " a lootbox/key, but they were offline!");
- }else{
- Player target = Bukkit.getServer().getPlayer(args[1]);
- ItemStack is = getConfigItemStack("options.items.lootbox-item");
- ItemStack is2 = getConfigItemStack("options.items.key-item");
- if(is!=null && is2!=null){
- String dispName = "";
- for(int i=3;i<args.length;i++){
- if(i==3){
- dispName = dispName + args[i];
- }else{
- dispName = dispName + " " + args[i];
- }
- }
- dispName = ChatColor.stripColor(dispName.replaceAll(" ", "_").replaceAll("&", "§"));
- if(args[2].equalsIgnoreCase("lootbox") || args[2].equalsIgnoreCase("box") || args[2].equalsIgnoreCase("chest")){
- if(getConfig().contains("lootboxes."+dispName)){
- ItemMeta im = is.getItemMeta();
- im.setDisplayName(getConfig().getString("lootboxes."+dispName+".color").replaceAll("&", "§") + dispName.replaceAll("_", " "));
- im.setLore(getConfigLore(getConfig().getString("lootboxes."+dispName+".lore"), false));
- is.setItemMeta(im);
- target.sendMessage(pre()+getConfig().getString("options.chat.given-lootbox").replaceAll("&", "§"));
- if(target.getInventory().firstEmpty()==-1){
- target.sendMessage(pre()+getConfig().getString("options.chat.full-inventory").replaceAll("&", "§"));
- Bukkit.getServer().getWorld(target.getWorld().getName()).dropItem(target.getLocation(), is);
- log.info(pre() + "Server console has given " + target.getName() + " a lootbox, but they had no room so the item dropped at their feet.");
- }else{
- target.getInventory().addItem(is);
- log.info(pre() + "Server console has given " + target.getName() + " a lootbox.");
- }
- }else{
- log.info(pre() + "Server console incorrectly executed the '/lootbox give' command, lootbox not given.");
- }
- }else if(args[2].equalsIgnoreCase("key")){
- if(getConfig().contains("keys."+dispName)){
- ItemMeta im = is2.getItemMeta();
- im.setDisplayName(getConfig().getString("keys."+dispName+".color").replaceAll("&", "§") + dispName.replaceAll("_", " "));
- im.setLore(getConfigLore(getConfig().getString("keys."+dispName+".lore"), false));
- is2.setItemMeta(im);
- target.sendMessage(pre()+getConfig().getString("options.chat.given-key").replaceAll("&", "§"));
- if(target.getInventory().firstEmpty()==-1){
- target.sendMessage(pre()+getConfig().getString("options.chat.full-inventory").replaceAll("&", "§"));
- Bukkit.getServer().getWorld(target.getWorld().getName()).dropItem(target.getLocation(), is2);
- log.info(pre() + "Server console has given " + target.getName() + " a key, but they had no room so the item dropped at their feet.");
- }else{
- target.getInventory().addItem(is2);
- log.info(pre() + "Server console has given " + target.getName() + " a key.");
- }
- }else{
- log.info(pre() + "Server console incorrectly executed the '/lootbox give' command, lootbox not given.");
- }
- }else{
- log.info(pre() + "Server console incorrectly executed the '/lootbox give' command, lootbox not given.");
- }
- }else{
- log.info(pre() + "Server console ran into a config.yml error while executing '/lootbox give' command, lootbox not given.");
- }
- }
- }else{
- log.warning(pre() + "Server console tried to send a command, but it was not properly used!");
- }
- }
- }
- return true;
- }
- @SuppressWarnings("deprecation")
- public String toLootboxString(ItemStack item) {
- // id, amn, eids, name, lore, chance
- if(item!=null && item.getType()!=Material.AIR){
- String fullString = "";
- if(item.getDurability()==0){
- fullString = fullString+item.getTypeId();
- }else{
- fullString = fullString+item.getTypeId()+":"+item.getDurability();
- }
- fullString = fullString+" "+item.getAmount();
- int chance = -1;
- if(item.hasItemMeta()){
- ItemMeta im = item.getItemMeta();
- if(im.hasLore()){
- String loreString = "";
- ArrayList<String> lore = (ArrayList<String>) im.getLore();
- if(lore.get(0).startsWith("§7") && lore.get(0).endsWith("% Chance")){
- String[] split = lore.get(0).split("%");
- String tempString = split[0].replaceAll("§7", "");
- boolean go=true;
- try{
- chance = Integer.parseInt(tempString);
- }catch(NumberFormatException e){
- go = false;
- }
- if(!go || chance>100 || chance <0){
- chance = -1;
- }
- }else{
- loreString = lore.get(0).replaceAll("§", "&").replaceAll(" ", "_");
- }
- for(int i=1;i<lore.size();i++){
- if(lore.get(i).startsWith("§7") && lore.get(i).endsWith("% Chance")){
- String[] split = lore.get(i).split("%");
- String tempString = split[0].replaceAll("§7", "");
- boolean go=true;
- try{
- chance = Integer.parseInt(tempString);
- }catch(NumberFormatException e){
- go = false;
- }
- if(!go || chance>100 || chance <0){
- chance = -1;
- }
- }else{
- if(loreString==""){
- loreString = lore.get(i).replaceAll("§", "&").replaceAll(" ", "_");
- }else{
- loreString = loreString+"|"+lore.get(i).replaceAll("§", "&").replaceAll(" ", "_");
- }
- }
- }
- if(loreString!=""){
- fullString = fullString+" lore:"+loreString.replaceAll(" ", "_");
- }
- if(chance!=-1){
- fullString = fullString+" chance:"+chance;
- }
- }
- if(im.hasDisplayName()){
- fullString = fullString+" name:"+im.getDisplayName().replaceAll("§", "&").replaceAll(" ", "_");
- }
- if(im.hasEnchants()){
- Iterator<Entry<Enchantment, Integer>> it = im.getEnchants().entrySet().iterator();
- while (it.hasNext()) {
- Entry<Enchantment, Integer> pairs = it.next();
- fullString = fullString+" "+pairs.getKey().getId()+":"+pairs.getValue();
- }
- }
- }
- return fullString;
- }else{
- return null;
- }
- }
- @EventHandler
- public void onInventoryClick(InventoryClickEvent event){
- if(event.getInventory().getName().equals("Lootbox Preview")){
- event.setCancelled(true);
- }
- }
- @SuppressWarnings({ "unchecked", "unused" })
- @EventHandler
- public void onPlayerInteract(PlayerInteractEvent event){
- if(event.getAction() == Action.RIGHT_CLICK_BLOCK || event.getAction() == Action.RIGHT_CLICK_AIR){
- ItemStack iih = event.getPlayer().getItemInHand();
- if(isKey(iih)){
- ItemMeta iihm = iih.getItemMeta();
- String sn = ChatColor.stripColor(iihm.getDisplayName()).replaceAll(" ", "_");
- if(event.getItem().getAmount()==1){
- String rn = sn.replaceAll("_", " ");
- rn = getConfig().getString("keys."+sn+".color").replaceAll("&", "§") + rn;
- ArrayList<String> testLore = (ArrayList<String>) iihm.getLore();
- ArrayList<String> configLore = getConfigLore(getConfig().getString("keys."+sn+".lore"), false);
- if(testLore.equals(configLore) && rn.equalsIgnoreCase(iihm.getDisplayName())){
- event.setCancelled(true);
- if(isPermissible(event.getPlayer(), "use.key." + getConfig().getString("keys."+sn+".permission"), true)){
- ArrayList<String> n = (ArrayList<String>) getConfig().getList("keys."+sn+".opens-lootboxes");
- if(n==null || n.size()==0){
- event.getPlayer().sendMessage(pre()+getConfig().getString("options.chat.nothing").replaceAll("&", "§"));
- }else{
- Inventory inv = event.getPlayer().getInventory();
- ItemStack stack = new ItemStack(getConfigItemStack("options.items.lootbox-item"));
- String save = "";
- int slot=0;
- boolean hasLB=false;
- for(int i2=0;i2<inv.getSize();i2++){
- if(!hasLB){
- if(inv.getItem(i2)!=null && inv.getItem(i2).getType().equals(stack.getType()) && inv.getItem(i2).getAmount()==1){
- if(inv.getItem(i2).getItemMeta().hasLore() && inv.getItem(i2).getItemMeta().hasDisplayName()){
- for(int i3=0;i3<n.size();i3++){
- 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))){
- Player p = event.getPlayer();
- ArrayList<String> list = (ArrayList<String>) getConfig().getList("lootboxes."+n.get(i3)+".items");
- if(getConfig().getList("lootboxes."+n.get(i3)+".items")==null || canFitItems(p.getInventory(), getLootboxItems(n.get(i3)))){
- save=n.get(i3);
- slot = i2;
- hasLB=true;
- }else{
- //TODO Rename? Could be sent multiple times
- p.sendMessage(pre()+getConfig().getString("options.chat.full-inventory").replaceAll("&", "§"));
- }
- }
- }
- }
- }
- }
- }
- if(hasLB){
- String path = save;
- Player p = event.getPlayer();
- p.getInventory().setItem(slot, null);
- openLootbox(event.getPlayer(), save);
- }else{
- event.getPlayer().sendMessage(pre()+getConfig().getString("options.chat.no-lootbox-to-open").replaceAll("&", "§"));
- }
- }
- }
- }
- }else{
- event.getPlayer().sendMessage(pre()+getConfig().getString("options.chat.more-than-one-key").replaceAll("&", "§"));
- }
- }else if(isLootbox(iih)){
- ItemMeta iihm = iih.getItemMeta();
- String sn = ChatColor.stripColor(iihm.getDisplayName()).replaceAll(" ", "_");
- event.setCancelled(true);
- String rn = sn.replaceAll("_", " ");
- rn = getConfig().getString("lootboxes."+sn+".color").replaceAll("&", "§") + rn;
- if(getConfig().getBoolean("lootboxes."+sn+".requires-a-key")){
- event.getPlayer().sendMessage(pre()+getConfig().getString("options.chat.no-key").replaceAll("&", "§"));
- }else if(isPermissible(event.getPlayer(), "use.lootbox." + getConfig().getString("lootboxes."+sn+".permission"), true)){
- openLootbox(event.getPlayer(), sn);
- }
- }
- }
- }
- @SuppressWarnings({ "deprecation", "unchecked" })
- private void openLootbox(Player p, String path){
- //Check lists
- if(p.getItemInHand().getAmount()==1){
- //Check permissions
- if(isPermissible(p, "use.lootbox."+getConfig().getString("lootboxes."+path+".permission"), true)){
- //Check if no items
- if(getConfig().getList("lootboxes."+path+".items")!=null && getConfig().getList("lootboxes."+path+".items").isEmpty()){
- p.sendMessage(pre()+getConfig().getString("options.chat.empty-lootbox").replaceAll("&", "§"));
- }else{
- ArrayList<ItemStack> items = getLootboxItems(path);
- if(getConfig().getList("lootboxes."+path+".items")==null || canFitItems(p.getInventory(), items)){
- p.getInventory().setItemInHand(new ItemStack(Material.AIR));
- //execute-commands from lootbox config section
- if(getConfig().getList("lootboxes."+path+".execute-commands")!=null){
- ArrayList<String> eC = (ArrayList<String>) getConfig().getList("lootboxes."+path+".execute-commands");
- execComm(p, eC);
- }
- if(getConfig().getString("lootboxes."+path+".open-message")!=null){
- p.sendMessage(pre()+getConfig().getString("lootboxes."+path+".open-message").replaceAll("&", "§"));
- }
- if(getConfig().getString("lootboxes."+path+".broadcast-message")!=null){
- Bukkit.getServer().broadcastMessage(pre()+getConfig().getString("lootboxes."+path+".broadcast-message").replaceAll("&", "§").replaceAll("\\{PLAYERNAME}", p.getName()));
- }
- if(Boolean.parseBoolean(getConfig().getString("options.other.make-noise-when-lootbox-opens"))){
- p.playSound(p.getLocation(), Sound.FIREWORK_LAUNCH, 1,0);
- }
- if(getConfig().getList("lootboxes."+path+".items")!=null){
- ArrayList<Integer> chances = getLootboxChance(path);
- ArrayList<ItemStack> mustItems = new ArrayList<ItemStack>();
- ArrayList<ItemStack> otherItems = new ArrayList<ItemStack>();
- ArrayList<ItemStack> leastItems = new ArrayList<ItemStack>();
- for(int i=0;i<items.size();i++){
- int chance = chances.get(i);
- rand = new Random();
- int num = rand.nextInt(100)+1;
- if(chance == 100){
- otherItems.add(items.get(i));
- }else if(chance >= num){
- mustItems.add(items.get(i));
- }else{
- leastItems.add(items.get(i));
- }
- }
- if(getMaxItems(path)==-1 || getMinItems(path)==-1){
- p.sendMessage(pre()+getConfig().getString("options.chat.config-error").replaceAll("&", "§"));
- }else{
- ArrayList<ItemStack> giveItems = getItemsToGive(getMinItems(path), getMaxItems(path), mustItems, otherItems, leastItems);
- for(int i=0;i<giveItems.size();i++){
- p.getInventory().addItem(giveItems.get(i));
- }
- }
- }
- }else{
- //full inv
- p.sendMessage(pre()+getConfig().getString("options.chat.full-inventory").replaceAll("&", "§"));
- }
- }
- p.updateInventory();
- }
- }else{
- //idiot-proofing
- if(p.getItemInHand().getAmount()!=1){
- p.sendMessage(pre()+getConfig().getString("options.chat.more-than-one-lootbox").replaceAll("&", "§"));
- }else{
- p.sendMessage(pre()+getConfig().getString("options.chat.config-error").replaceAll("&", "§"));
- }
- }
- }
- private boolean isPermissible(Player p, String perm, boolean ret){
- //permissions checker
- if(p.hasPermission("lootbox."+perm.toLowerCase()) || p.isOp()){
- return true;
- }else if(ret){
- p.sendMessage(pre()+getConfig().getString("options.chat.no-permissions").replaceAll("&", "§"));
- return false;
- }else{
- return false;
- }
- }
- private ArrayList<String> getConfigLore(String s, boolean rep){
- ArrayList<String> lore = new ArrayList<String>();
- if(s.contains("|")){
- //split lore
- String[] loreList = s.split("\\|");
- for(int i=0;i<loreList.length;i++){
- if(rep){
- lore.add(loreList[i].replaceAll("&", "§").replaceAll("_", " "));
- }else{
- lore.add(loreList[i].replaceAll("&", "§"));
- }
- }
- }else{
- //single line lore
- if(rep){
- lore.add(s.replaceAll("&", "§").replaceAll("_", " "));
- }else{
- lore.add(s.replaceAll("&", "§"));
- }
- }
- return lore;
- }
- @SuppressWarnings("deprecation")
- private ItemStack getConfigItemStack(String path){
- //CAN RETURN NULL
- //Construct them itemstack
- String mat = getConfig().getString(path);
- if(mat.contains(":")){
- //if has a dmg value
- String[] matList = mat.split(":");
- if(matList.length!=2){
- return null;
- }else{
- boolean go = true;
- int id1 = 54;
- int id2 = 0;
- //parse from string
- try{
- id1=Integer.parseInt(matList[0]);
- }catch(NumberFormatException e){
- e.printStackTrace();
- go = false;
- }
- try{
- id2=Integer.parseInt(matList[1]);
- }catch(NumberFormatException e){
- e.printStackTrace();
- go = false;
- }
- if(go){
- //construct item with dmg
- ItemStack item = new ItemStack(Material.getMaterial(id1));
- item.setDurability((short) id2);
- return item;
- }else{
- return null;
- }
- }
- }else{
- //try parsing material id from string
- int matId = 54;
- boolean go = true;
- try{
- matId = Integer.parseInt(mat);
- }catch(NumberFormatException e){
- e.printStackTrace();
- go = false;
- }
- //construct
- if(go){
- return new ItemStack(Material.getMaterial(matId));
- }else{
- return null;
- }
- }
- }
- public String pre(){
- return getConfig().getString("options.chat.prefix").replaceAll("&", "§");
- }
- public void execComm(Player p, ArrayList<String> eC){
- for(int i6=0;i6<eC.size();i6++){
- String str = eC.get(i6);
- Boolean go3=true;
- String[] cSplit = eC.get(i6).split(" ");
- for(int i7=0;i7<cSplit.length;i7++){
- if(go3 && cSplit[i7].split(":").length==2){
- go3 = false;
- int chance2=0;
- try{
- chance2=Integer.parseInt(cSplit[i7].split(":")[1]);
- }catch(NumberFormatException e){
- log.info(pre()+"Part of an item's command's chance was not set correctly in the config, giving it a chance of 0.");
- }
- if(chance2!=0){
- Random rand2 = new Random();
- if(chance2 >= rand2.nextInt(100)+1){
- for(int i8=0;i8<cSplit.length;i8++){
- if(i8!=i7){
- if(i8==0){
- str = cSplit[i8];
- }else{
- str = str + " " + cSplit[i8];
- }
- }
- }
- Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), str.replaceAll("\\{PLAYERNAME}", p.getName()));
- }
- }
- }
- }
- if(go3){
- Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), str.replaceAll("\\{PLAYERNAME}", p.getName()));
- }
- }
- }
- @SuppressWarnings({ "unchecked", "deprecation" })
- public ArrayList<ItemStack> getLootboxItems(String path){
- if(getConfig().getList("lootboxes."+path+".items")==null){
- return null;
- }else{
- ArrayList<ItemStack> items = new ArrayList<ItemStack>();
- ArrayList<String> list = (ArrayList<String>) getConfig().getList("lootboxes."+path+".items");
- for(int i=0;i<list.size();i++){
- //Parse id,damage value, and amount from string
- String[] split = list.get(i).split(" ");
- ArrayList<String> lore = new ArrayList<String>();
- LinkedList<Integer> emap = new LinkedList<Integer>();
- LinkedList<Integer> amap = new LinkedList<Integer>();
- boolean go = true;
- int dmg=0;
- int id = 0;
- int amn=1;
- String name = null;
- if(split[0].contains(":")){
- String[] doubleSplit = split[0].split(":");
- try{
- id=Integer.parseInt(doubleSplit[0]);
- dmg=Integer.parseInt(doubleSplit[1]);
- }catch(NumberFormatException e){
- e.printStackTrace();
- go = false;
- }
- }else{
- try{
- id=Integer.parseInt(split[0]);
- }catch(NumberFormatException e){
- e.printStackTrace();
- go = false;
- }
- }
- try{
- amn=Integer.parseInt(split[1]);
- }catch(NumberFormatException e){
- e.printStackTrace();
- go = false;
- }
- if(go){
- for(int i2=2;i2<split.length;i2++){
- //if parsed ok, get enchantments, lore, and name
- String[] newSplit = split[i2].split(":", 2);
- int ench = 0;
- int eamn = 0;
- if(newSplit[0].equalsIgnoreCase("name")){
- //name
- name = newSplit[1].replaceAll("&", "§").replaceAll("_", " ");
- }else if(newSplit[0].equalsIgnoreCase("chance")){
- //Do nothing
- }else if(newSplit[0].equalsIgnoreCase("lore")){
- //lore
- lore = getConfigLore(newSplit[1], true);
- }else{
- //enchant and level
- try{
- ench = Integer.parseInt(newSplit[0]);
- eamn = Integer.parseInt(newSplit[1]);
- }catch(NumberFormatException e){
- e.printStackTrace();
- go = false;
- }
- if(go){
- emap.add(ench);
- amap.add(eamn);
- }
- }
- }
- //construct and add item
- ItemStack is = new ItemStack(Material.getMaterial(id), amn);
- ItemMeta im = is.getItemMeta();
- for(int i3=0;i3<amap.size();i3++){
- im.addEnchant(Enchantment.getById(emap.get(i3)), amap.get(i3), true);
- }
- im.setLore(lore);
- if(name!=null){
- im.setDisplayName(name);
- }
- is.setDurability((short) dmg);
- is.setItemMeta(im);
- items.add(is);
- }
- }
- return items;
- }
- }
- @SuppressWarnings("unchecked")
- public ArrayList<Integer> getLootboxChance(String path){
- ArrayList<Integer> chances = new ArrayList<Integer>();
- ArrayList<String> list = (ArrayList<String>) getConfig().getList("lootboxes."+path+".items");
- for(int i=0;i<list.size();i++){
- //Parse id,damage value, and amount from string
- String[] split = list.get(i).split(" ");
- int chance = 100;
- for(int i2=2;i2<split.length;i2++){
- String[] newSplit = split[i2].split(":", 2);
- if(newSplit[0].equalsIgnoreCase("chance")){
- //chance
- try{
- chance = Integer.parseInt(newSplit[1]);
- }catch(NumberFormatException e){
- log.info(pre()+"Part of an item's chance was not set correctly in the config, giving it a chance of 0.");
- chance = 0;
- }
- }
- }
- chances.add(chance);
- }
- return chances;
- }
- public boolean isLootbox(ItemStack iih){
- if(iih!=null && new ItemStack(iih.getType()).equals(getConfigItemStack("options.items.lootbox-item"))){
- ItemMeta iihm = iih.getItemMeta();
- if(iihm.hasDisplayName() && iihm.hasLore()){
- String sn = ChatColor.stripColor(iihm.getDisplayName()).replaceAll(" ", "_");
- if(getConfig().contains("lootboxes."+sn)){
- String rn = sn.replaceAll("_", " ");
- rn = getConfig().getString("lootboxes."+sn+".color").replaceAll("&", "§") + rn;
- ArrayList<String> testLore = (ArrayList<String>) iihm.getLore();
- ArrayList<String> configLore = getConfigLore(getConfig().getString("lootboxes."+sn+".lore"), false);
- if(testLore.equals(configLore) && rn.equalsIgnoreCase(iihm.getDisplayName())){
- return true;
- }else{
- return false;
- }
- }else{
- return false;
- }
- }else{
- return false;
- }
- }else{
- return false;
- }
- }
- public boolean isKey(ItemStack iih){
- if(iih!=null && new ItemStack(iih.getType()).equals(getConfigItemStack("options.items.key-item"))){
- ItemMeta iihm = iih.getItemMeta();
- if(iihm.hasDisplayName() && iihm.hasLore()){
- String sn = ChatColor.stripColor(iihm.getDisplayName()).replaceAll(" ", "_");
- if(getConfig().contains("keys."+sn)){
- String rn = sn.replaceAll("_", " ");
- rn = getConfig().getString("keys."+sn+".color").replaceAll("&", "§") + rn;
- ArrayList<String> testLore = (ArrayList<String>) iihm.getLore();
- ArrayList<String> configLore = getConfigLore(getConfig().getString("keys."+sn+".lore"), false);
- if(testLore.equals(configLore) && rn.equalsIgnoreCase(iihm.getDisplayName())){
- return true;
- }else{
- return false;
- }
- }else{
- return false;
- }
- }else{
- return false;
- }
- }else{
- return false;
- }
- }
- public boolean canFitItems(Inventory inv, ArrayList<ItemStack> items){
- //TODO Remove lootbox and key from list
- Inventory copy = Bukkit.getServer().createInventory(null, 36);
- for(int i=0;i<36;i++){
- if(inv.getItem(i)!=null){
- copy.addItem(inv.getItem(i));
- }
- }
- boolean can = true;
- for(int i=0;i<items.size();i++){
- if(!copy.addItem(items.get(i)).isEmpty()){
- can = false;
- }
- }
- copy.clear();
- return can;
- }
- public ArrayList<ItemStack> getItemsToGive(int min, int max, ArrayList<ItemStack> mustItems, ArrayList<ItemStack> otherItems, ArrayList<ItemStack> leastItems){
- ArrayList<ItemStack> returnList = new ArrayList<ItemStack>();
- if(mustItems.size()<min){
- returnList.addAll(mustItems);
- if(otherItems.size()+mustItems.size()+leastItems.size()<min){
- returnList.addAll(otherItems);
- returnList.addAll(leastItems);
- }else if(otherItems.size()+mustItems.size()<min){
- returnList.addAll(otherItems);
- for(int i=0;i<min-(mustItems.size()+otherItems.size());i++){
- Random rand = new Random();
- int num = rand.nextInt(leastItems.size());
- returnList.add(leastItems.get(num));
- leastItems.remove(num);
- }
- }else{
- for(int i=0;i<min-mustItems.size();i++){
- Random rand = new Random();
- int num = rand.nextInt(otherItems.size());
- returnList.add(otherItems.get(num));
- otherItems.remove(num);
- }
- }
- return returnList;
- }else if(mustItems.size()>max){
- for(int i=0;i<max;i++){
- Random rand = new Random();
- int num = rand.nextInt(mustItems.size());
- returnList.add(mustItems.get(num));
- mustItems.remove(num);
- }
- return returnList;
- }else if(mustItems.size()+otherItems.size()<max){
- returnList.addAll(mustItems);
- returnList.addAll(otherItems);
- return returnList;
- }else{
- return mustItems;
- }
- }
- public int getMaxItems(String path){
- boolean go = true;
- int max = 0;
- if(getConfig().get("lootboxes."+path+".max-items")==null){
- max = 100000;
- }else if(getConfig().get("lootboxes."+path+".max-items") instanceof Integer){
- max = getConfig().getInt("lootboxes."+path+".max-items");
- }else if(getConfig().get("lootboxes."+path+".max-items") instanceof String){
- try{
- max = Integer.parseInt(getConfig().getString("lootboxes."+path+".max-items"));
- }catch(NumberFormatException e){
- go = false;
- }
- }else{
- go = false;
- }
- if(go){
- return max;
- }else{
- return -1;
- }
- }
- public int getMinItems(String path){
- boolean go = true;
- int min = 0;
- if(getConfig().get("lootboxes."+path+".min-items")==null){
- min = 0;
- }else if(getConfig().get("lootboxes."+path+".min-items") instanceof Integer){
- min = getConfig().getInt("lootboxes."+path+".min-items");
- }else if(getConfig().get("lootboxes."+path+".min-items") instanceof String){
- try{
- min = Integer.parseInt(getConfig().getString("lootboxes."+path+".min-items"));
- }catch(NumberFormatException e){
- go = false;
- }
- }else{
- go = false;
- }
- if(go){
- return min;
- }else{
- return -1;
- }
- }
- public boolean hasItemInHand(ItemStack iih){
- if(!iih.equals(null) && !iih.getType().equals(Material.AIR)){
- return true;
- }else{
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement