[Tutorial] One-Line ItemStack-Method

Discussion in 'Spigot Plugin Development' started by HardTimeStudio, May 3, 2015.

?

What do you think about this Method?

  1. Very nice, I'll use it.

    6 vote(s)
    46.2%
  2. Nah, I don't like it. This is to complicated.

    7 vote(s)
    53.8%
  1. Hello guys,
    today I'll be showing you how to create a method with which you can create a custom ItemStack with one line of code. So, lets start.

    Create a custom ItemStack-Method

    First of all we need a method which recieves an Material, an Integer, a Short, a String, and a String which recieves unlimited amount of Strings.
    Code (Text):
    public static ItemStack createCustomItem(Material customMaterial, int customAmount, short customSubId, String customDisplayName, String... customSubtitle) {
    In this method we need a ItemStack. This one recieves the Material, the Integer for the Amount and the Short for the SubID, from the method.
    Code (Text):
    ItemStack customItemStack = new ItemStack(customMaterial, customAmount, customSubId);
    Then we need the ItemMeta to define the Display Name. This part recieves the String which defines the Display Name.
    Code (Text):
    ItemMeta customItemMeta = customItemStack.getItemMeta();
    customItemMeta.setDisplayName(customDisplayName);
    After this we want to have the subtitle. Therefore we need a little for-loop to get the amount of Strings, The customSubtitle-String recieved.
    Code (Text):
    List<String> customLore = new ArrayList<String>();
    for (String customSubtitle1 : customSubtitle) {
        customLore.add(customSubtitle1);
    }
    The last thing we have to do is setting the Lore and the ItemMeta.
    Code (Text):
    customItemMeta.setLore(customLore);
    customItemStack.setItemMeta(customItemMeta);
    Then the Method is ready to return the custom ItemStack.
    Code (Text):
    return customItemStack;
    So the complete Method will look like this:
    Code (Text):
    public static ItemStack createCustomItem(Material customMaterial, Integer customAmount, Short customSubId, String customDisplayName, String... customSubtitle) {
         
        ItemStack customItemStack = new ItemStack(customMaterial, customAmount, customSubId);
        ItemMeta customItemMeta = customItemStack.getItemMeta();
        customItemMeta.setDisplayName(customDisplayName);
        List<String> customLore = new ArrayList<String>();
        for (String customSubtitle1 : customSubtitle) {
            customLore.add(customSubtitle1);
        }
        customItemMeta.setLore(customLore);
        customItemStack.setItemMeta(customItemMeta);
         
        return customItemStack;
    }
    Example
    Code (Text):
    p.getInventory.setItem(0, createCustomItem(Material.COMPASS, 9, (short) 0, "This is a Compass", ChatColor.GRAY + "Right-Click, to say", ChatColor.RED + "Hello World");
    Creates this:
    SimpleItemStackExample.png
     
    #1 HardTimeStudio, May 3, 2015
    Last edited: May 3, 2015
  2. @HardTimeStudio
    • Use the primitive types rather than wrapper types (short vs Short, int vs Integer)
    • Use ChatColors rather than section signs.
    • You can use Arrays.asList instead of looping over the array and adding each element individually - Arrays.asList also happens to be faster than inserting it manually.
     
    • Agree Agree x 1
  3. @DarkSeraphim
    • Yes, I totally forgot abount it. Seems like I was Brain-AFK for a moment.
    • I don't see any difference in using ChatColor instead of ColorCodes o_O
    • Never thought about Array.asList. Think this is a good idea. :D
    Thank you for giving me those tips so I can improve my Method.
     
  4. Aside that their meaning is encapsulated (section signs have no identifyable meaning in the code, while ChatColors do), it also won't break if Minecraft / Bukkit ever decides to change the character.
     
    • Like Like x 1
  5. @DarkSeraphim Thanks for the Explanation.:)
    I allready changed it in my post.