Solved \newline adding spaces instead of a newline for custom item

Discussion in 'Spigot Plugin Development' started by Upgradingmaster, Feb 21, 2020.

  1. Hello!
    I'm a beginner to spigot plugin development but have basic knowledge in Java.

    I was watching a tutorial for creating custom items. He just showed the basics. I wanted to make a gold sword with a description as shown in the picture(Picture 1). When I entered the string for the description and exported it, it showed me (Picture 2). The \n hadn't worked and had not gone to the next line and simply added spaces after it (the number of spaces applied are based on the number of \n's I add for some reason). I've tried switching the \n to \r, %r, and %n and it remains the same.
    PS: Sorry for the long description in the code :/

    Midas OG.jpg

    2020-02-21_22.37.48.png







    Code (Java):
    package com.upm.firstPlugin;

    import java.util.ArrayList;

    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.inventory.ItemFlag;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;

    public class ItemEditor {
     
        String midasDesc1 = ("Damage: " + ChatColor.RED + "+152\n" + ChatColor.RESET + "Strength: " + ChatColor.RED + "+2\n\n" + ChatColor.GOLD + "Item Ability: Greed\n" + ChatColor.RESET + "The strength and damage bonus of this item is dependant on the price paid for it at the " + ChatColor.DARK_PURPLE + "Dark Auction!\n" + ChatColor.RESET + "The maximum bonus of this item is " + ChatColor.RED + "120" + ChatColor.RESET + "if the bid was " + ChatColor.GOLD + "50,000,000 " + ChatColor.RESET + "or higher!\n\n" + "Price Paid: " + ChatColor.GOLD + "100,000 Coins\n" + ChatColor.RESET + "Strength Bonus: " + ChatColor.RED + "2\n" + ChatColor.RESET + "Damage Bonus: " + ChatColor.RED + "2\n\n" + ChatColor.GOLD + "" + ChatColor.BOLD + "LEGENDARY");
       //ERROR HERE ^^^^^^
     
        public ItemStack makeItem(Material m, String name, String desc, int amount) {
     
         
            //Creates the item
            ItemStack item = new ItemStack(m, amount);
         
            //Gets the information of the item
            ItemMeta im = item.getItemMeta();
         
            //Sets Name
            im.setDisplayName(name);
            //Sets Description(Lore)
            ArrayList<String> lore = new ArrayList<String>();
            lore.add(desc);
            im.setLore(lore);
         
         
            //Hide default ToolTip
            im.addItemFlags(ItemFlag.HIDE_ENCHANTS);
            im.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
         
            //Sets the im to the actual meta
            item.setItemMeta(im);
         
            return item;
        }
     
     
        //Making the item
        public ItemStack buildMidas(int amount) {
         
            Material m = Material.GOLD_SWORD;
            String name = (ChatColor.GOLD + "Midas' Sword");
            String desc = midasDesc1;
         
            return makeItem(m, name, desc, amount);
         
        }
     
    }
     
     
    #1 Upgradingmaster, Feb 21, 2020
    Last edited: Feb 22, 2020
  2. Lore is a List of strings, each new item in the list is a new line. So for each new line, put it in a different lore.add()
     
    #2 Broccolai, Feb 21, 2020
    Last edited: Feb 21, 2020
    • Agree Agree x 2
  3. I agree with Broccolai
    but you can try if you wish with this:
    Code (Java):
    String midasDesc1 = ("Damage: " + ChatColor.RED + "+152\n" + ChatColor.RESET + "Strength: " + ChatColor.RED + "+2\n\n" + ChatColor.GOLD + "Item Ability: Greed\n" + ChatColor.RESET + "The strength and damage bonus of this item is dependant on the price paid for it at the " + ChatColor.DARK_PURPLE + "Dark Auction!\n" + ChatColor.RESET + "The maximum bonus of this item is " + ChatColor.RED + "120" + ChatColor.RESET + "if the bid was " + ChatColor.GOLD + "50,000,000 " + ChatColor.RESET + "or higher!\n\n" + "Price Paid: " + ChatColor.GOLD + "100,000 Coins\n" + ChatColor.RESET + "Strength Bonus: " + ChatColor.RED + "2\n" + ChatColor.RESET + "Damage Bonus: " + ChatColor.RED + "2\n\n" + ChatColor.GOLD + "" + ChatColor.BOLD + "LEGENDARY").replaceAll("\\n", "\n");
    I'm not sure it works with the object description, but you can try :)
     
  4. I still don't know where to put the lore.add() and what do I put here(code)

    Code (Java):
        //Making the item
        public ItemStack buildMidas(int amount) {
           
            Material m = Material.GOLD_SWORD;
            String name = (ChatColor.GOLD + "Midas' Sword");
            String desc = //What do I type here then    <======
           
            return makeItem(m, name, desc, amount);
           
        }
     
  5. ItemStack#getItemMeta()
    ItemMeta#setLore(List<String>)
    ItemStack#setItemMeta(ItemMeta)
     
    • Agree Agree x 1
  6. i have done this already right?I just want to know what to add here


    Code (Java):
        //Making the item
        public ItemStack buildMidas(int amount) {
         
            Material m = Material.GOLD_SWORD;
            String name = (ChatColor.GOLD + "Midas' Sword");
            String desc = //What do I type here then    <======
         
            return makeItem(m, name, desc, amount);
         
        }
     
  7. Blindly following tutorials is never a good idea, regardless, this method to create an item seems outdated, it needs to be slightly altered to accommodate item meta, and hence allow the creation of a "description"

    Here is how you can do it, ill let you figure out where it goes, maybe scrap the method entirely:

    Code (Text):
    public ItemStack buildMidas(int amount) {

            ItemStack item = new ItemStack(Material.GOLDEN_SWORD, amount); <-- initilize a new itemstack object, dont need a seperate "makeitem" method to do this
            String name = (ChatColor.GOLD + "Midas' Sword"); <-- name as you had it
            List<String> description = new ArrayList<>(); <-- create a new list of strings, each entry is a different line
            description.add(0, "First line"); <-- line 1...
            description.add(1, "Second line"); <-- 2..
            description.add(2, "Third line..."); <-- and so on...
         
            ItemMeta itemMeta = item.getItemMeta(); <-- to add our description, name etc we need to get its itemmeta
            itemMeta.setDisplayName(name); <-- set our name
            itemMeta.setLore(description); <-- set our description

    (with itemmeta, you can also explore adding enchantments and traits)

            item.setItemMeta(itemMeta); <-- and you must set the now modified itemmeta back to the item
         
            return item; <-- and return this item

        }
     



  8. Thank you so much for this reply I have got the code to work now. I was actually following the spigot official wiki and I had created the makeItem method to make it easier for me to create more custom items in the future as I could just add the material, name, description and amount but I guess this will do for now. Ty :)
     
    • Friendly Friendly x 1