Util [Helper Methods] Get Essentials Kits

Discussion in 'Resources' started by RedmanCometh, Nov 30, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    RedmanCometh

    So I was messing with essentials in one of my plugins, and I wanted to use it for kits. The server already used essentials, so I figured it would be nice and easy. It seemed pointless to code already-existent functionality...anyways it turned out to be a POTA. So I dug into the essentials source, grabbed the relevant code, and made a quick method.

    So here is a method for giving a player a kit from just a string of the kit's name...it may not be the best way to do things, but it's async, and seems to simplify it greatly.

    Code:
        public void getKit(Player p, String name)
        {
        IEssentials ess = (Essentials) Bukkit.getPluginManager().getPlugin("Essentials");
        ForkJoinPool.commonPool().execute(new Runnable()
        {
            public void run()
            {
            if (ess != null)
            {
                Map<String, Object> kit = ess.getSettings().getKit(name.toLowerCase());
                User u = ess.getUser(p);
                List<String> items;
                try
                {
                items = Kit.getItems(ess, u, name, kit);
                p.getInventory().addItem(deSerialize(items, u).get());
                }
                catch (Exception e)
                {
                e.printStackTrace();
                }
                p.updateInventory();
            }
            else
            {
                System.out.println("essentials was null when getting kits!");
            }
            }
        });
        }
     
        public CompletableFuture<ItemStack[]> deSerialize(List<String> items, User user)
        {
        CompletableFuture<ItemStack[]> finalList = new CompletableFuture<>();
        List<ItemStack> itemList = new ArrayList<ItemStack>();
        IText input = new SimpleTextInput(items);
        IText output = new KeywordReplacer(input, user.getSource(), ess);
        for (String kitItem : output.getLines())
        {
            String[] parts = kitItem.split(" +");
            try
            {
            ItemStack parseStack = ess.getItemDb().get(parts[0], parts.length > 1 ? Integer.parseInt(parts[1]) : 1);
            if (parseStack != null && parseStack.getType() != Material.AIR)
            {
                MetaItemStack metaStack = new MetaItemStack(parseStack);
                if (parts.length > 2)
                {
                metaStack.parseStringMeta(null, true, parts, 2, ess);
                }
                itemList.add(metaStack.getItemStack());
            }
            }
            catch (Exception e)
            {
            e.printStackTrace();
            }
        }
        finalList.complete(itemList.toArray(new ItemStack[itemList.size()]));
        return finalList;
        }
    
    (You might want to fix the formatting if you use this..for some reason it got messed up when I pasted this in)
     
  2. Offline

    ChipDev

    Why not just save a hashmap<OfflinePlayer, String>();?
     
  3. Offline

    RedmanCometh

    I'm not actually sure where you're referring to using that, but storing a Player object as a key in a map shouldn't be done. It should be a UUID or some kind of ID. Also the player could be completely taken out of the picture in the get method, and used in a set method.

    I just grabbed this from my plugin (except the name string is from an enum not a param, and essentials is instantiated statically in the top of the class)
     
Thread Status:
Not open for further replies.

Share This Page