[Util] SimplyItems

Discussion in 'Resources' started by sgavster, Nov 6, 2013.

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

    sgavster

    I was recently playing around with ItemMeta and things, and I noticed how much of a pain it is to give players items with ItemMeta and Lores, ect. I came up with a method to make it easy.

    This is the code:


    PHP:
        public ItemStack setNameAndLore(Material materialint amountString nameString... lore)
        {
            
    ItemStack item = new ItemStack(materialamount);
            
    ItemMeta meta item.getItemMeta();
            
    meta.setDisplayName(ChatColor.translateAlternateColorCodes('&'name));
            
    ArrayList<Stringlorez = new ArrayList<String>();
            for(
    String mylores lore)
            {
                
    lorez.add(ChatColor.translateAlternateColorCodes('&'mylores));
            }
            
    meta.setLore(lorez);
            
    item.setItemMeta(meta);
            return 
    item;
        }
    To use this you can do

    PHP:
    //something like player.getInventory().addItem, or something else, like dropping it on the ground.
    setNameAndLore(Material.YOUR_MATERIAlamount"Name""Lore 1""Lore 2""And as many more lores as you want!");
     
    You can use colors, like "&6" for gold.

    An example would be:


    PHP:
    player.getInventory().addItem(setNameAndLore(Material.DIAMOND_BLOCK1"&aMy awesome diamond block""&6It's so cool""&4You cry when you see it!"));
    let me know if it's useful :)

    Change Log (Click to See) (open)

    v1.0
    First release
    v1.1
    Added amount
     
    ccrama, Skyost and maxben34 like this.
  2. Offline

    maxben34

    This is pretty awesome if it works right :). Good job!
     
  3. Offline

    sgavster

    maxben34 Thanks! :)
    And man, that speedy response! :p
     
  4. Offline

    maxben34

    Hahaah. I just got lucky that I noticed it. I just roam the plugin development while waiting for help with one of my own :p. It's fun helping others and I get to learn more from it too.
     
    sgavster likes this.
  5. Offline

    DarkBladee12

    sgavster I think if you've learned java and know how to deal with the Bukkit API it should be no problem to think of a method like that... And I don't really think it's a pain to get the ItemMeta object from the ItemStack, use 2 methods and set the ItemMeta of the ItemStack again. If you would have to deal with NMS it's more likely a pain to change the NBTTagCompound, but since Bukkit implemented this it isn't^^
     
    Wingzzz and JPG2000 like this.
  6. Offline

    sebasju1234

    What I would do, is change this line:
    Code:
        public ItemStack setNameAndLore(Material material, String name, String... lore)
    To this:
    Code:
        public ItemStack setNameAndLore(Material material, String name, String[] lore)
    So that you could use it like this:
    Code:
    //something like player.getInventory().addItem, or something else, like dropping it on the ground.
    setNameAndLore(Material.YOUR_MATERIAl, "Name", new String[] { "lore", "lore 2" });
    But that's my opinion, haha.
     
    Skyost likes this.
  7. Offline

    Skyost

    Very basic but I like it :)
     
  8. Offline

    sgavster

    DarkBladee12 - Yes, it's simple to do, but if you're messing with a ton of items with ItemMeta, this makes it easy. sebasju1234 - I could, but I like how you can do "item, name, lore, lore, lore, lore"
    Skyost - Thank you :)
     
  9. Offline

    iZanax

    I would break this down to even 2 attributes
    (ItemStack item, String... name)
    That will make it able to use the same method for items without a lore.
     
  10. Offline

    DarkBladee12

    iZanax That's not true, you can even use "setNameAndLore(ItemStack item, String name, String... lore)" without a lore, since "Object..." doesn't require an input, so you could simply do "setNameAndLore(new ItemStack(Material.APPLE), "Super Apple")"!
     
  11. Offline

    iZanax

    DarkBladee12 Ow yea, you are right.
    Nevermind then. I forgot that essential part : (
     
  12. Offline

    JPG2000

    sgavster Your post pretty much the exact tutorial/post I made. Also, it's not hard to think of these. :p
     
  13. Offline

    sgavster

    JPG2000 The reason I made this was because I needed to make 50+ items with itemmeta on my server. I never said this was super complex or anything, just a little bit useful. And I don't know if you're implying that I copied you, but I've never even seen your post :(
     
    bobacadodl likes this.
  14. Offline

    JPG2000

    sgavster Your point is valid, and I do see the use. Sorry for being rude :(
     
  15. Offline

    JPG2000

    sgavster You can make the code faster and shorter without the loop:
    Code:
     public ItemStack setNameAndLore(Material material, String name, String... lore)
        {
            ItemStack item = new ItemStack(material);
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', name));
           
            meta.setLore(Arrays.asList(lore));
            item.setItemMeta(meta);
            return item;
        }
    I just found this out, since I never knew that adding the ... (forgot the name) was actually making an Array
     
  16. Offline

    sgavster

    JPG2000 Hmm, never thought of that. I'll edit the post in a bit. :)
     
    JPG2000 likes this.
  17. Offline

    sgavster

    JPG2000 Actually, I can't. Not with ChatColor.translateBlahBlah(blahblah) :(
     
  18. Offline

    JPG2000

    sgavster What? Just change up the code to my fix. It saves some memory and gets rid of useless code.
     
  19. Offline

    sgavster

    JPG2000 You can't use an array with ChatColor.translateAlterniblahblah(char, string);
     
  20. Offline

    JPG2000

    sgavster Add the lore like normal (how I said). Get rid of the translate feature. In my opinion it's stupid and worthless, be a use most developers I know just use the chat color enum. And mine is ssignificantly faster
     
  21. Offline

    Cirno

    Isn't this just the case of micro-optimization? Iterating an ArrayList is O(n), Arrays.asList is O(1), but you can't perform operations while using asList, unless you asList and then iterate over that (which is really pointless).
     
    sgavster likes this.
Thread Status:
Not open for further replies.

Share This Page