Add Lore To Item

Discussion in 'Plugin Development' started by ItsMees, Nov 11, 2013.

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

    ItsMees

    Hi Guys,
    I got an problem.
    I want that players get an item with the name WorldPainter and the lore will we arg 2 of the command.
    I tryd this code but is doesnt work!
    Code:java
    1. ItemStack wp = new ItemStack(Material.STICK, 1);
    2. ItemMeta im = wp.getItemMeta();
    3. im.setDisplayName(ChatColor.RED + "World Painter");
    4. lore = im.getLore();
    5. System.out.println(lore);
    6. lore.add(args[0]);
    7. im.setLore(lore);
    8. wp.setItemMeta(im);
    9. player.getPlayer().getInventory().addItem(wp);

    I hope someone can help me out!
     
  2. Offline

    xTonyDude

    Okay so you pretty much did everything right , The Error: is on - Line 9 , For Ex.

    Your Error , Line 9 , You Gaved an Unknown Item.
    Code:java
    1. player.getPlayer().getInventory().addItem(wp);


    Try Telling the server about the new ItemStack you maked, Ex.
    Code:java
    1. player.getInventory().addItem(new ItemStack[] { wp });


    That's The Only Error I Found , If You Still have an Error or Something, just Message me.
     
  3. Offline

    ItsMees

    It tryd it!
    The code will now look like this:
    Code:java
    1. ItemStack wp = new ItemStack(Material.STICK, 1);
    2. ItemMeta im = wp.getItemMeta();
    3. im.setDisplayName(ChatColor.RED + "World Painter");
    4. wp.setItemMeta(im);
    5.  
    6. lore = im.getLore();
    7. System.out.println(lore);
    8. lore.add(args[0]);
    9. im.setLore(lore);
    10.  
    11. player.getInventory().addItem(new ItemStack[] { wp });


    But when I test it wil gives this error:
    Code:
    12:46:18 [INFO] null
    12:46:18 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'pain
    ter' in plugin WorldPainter v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:19
    2)
            at org.bukkit.craftbukkit.v1_6_R3.CraftServer.dispatchCommand(CraftServe
    r.java:528)
            at net.minecraft.server.v1_6_R3.PlayerConnection.handleCommand(PlayerCon
    nection.java:968)
            at net.minecraft.server.v1_6_R3.PlayerConnection.chat(PlayerConnection.j
    ava:886)
            at net.minecraft.server.v1_6_R3.PlayerConnection.a(PlayerConnection.java
    :837)
            at net.minecraft.server.v1_6_R3.Packet3Chat.handle(SourceFile:49)
            at net.minecraft.server.v1_6_R3.NetworkManager.b(NetworkManager.java:296
    )
            at net.minecraft.server.v1_6_R3.PlayerConnection.e(PlayerConnection.java
    :116)
            at net.minecraft.server.v1_6_R3.ServerConnection.b(SourceFile:37)
            at net.minecraft.server.v1_6_R3.DedicatedServerConnection.b(SourceFile:3
    0)
            at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:5
    92)
            at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:2
    27)
            at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:4
    88)
            at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java
    :421)
            at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:5
    83)
    Caused by: java.lang.NullPointerException
            at com.itsmees.worldpainter.commands.CommandExecutor_Painter.onCommand(C
    ommandExecutor_Painter.java:57)
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
            ... 15 more
    
    Maybe are there args wrong!
    The command is: /painter <ID>
     
  4. Offline

    xTonyDude

    ItsMees Did you add the @EventHandler on top For Ex.
    Code:java
    1. @EventHandler
    2. ItemStack wp = new ItemStack(Material.STICK, 1);
    3. ItemMeta im = wp.getItemMeta();
    4. im.setDisplayName(ChatColor.RED + "World Painter");
    5. wp.setItemMeta(im);
    6.  
    7. lore = im.getLore();
    8. System.out.println(lore);
    9. lore.add(args[0]);
    10. im.setLore(lore);
    11.  
    12. player.getInventory().addItem(new ItemStack[] { wp });


    *Remember* to have imported implements Listener.
     
  5. Offline

    SmellyPenguin

    Code:java
    1. ItemStack item = new ItemStack(Material.STICK, 1);
    2. if(item.hasItemMeta()) {
    3. ItemMeta im = item.getItemMeta();
    4. ArrayList<String> lore = new ArrayList<String>();
    5. lore.add("Lore goes here");
    6. im.setLore(lore);
    7. item.setItemMeta(im);
    8. }


    Wrote this without an IDE, might've gotten some methods wrong or something
     
  6. Offline

    qlimax5000

    Code:java
    1. lore.add(args[0].toString());


    Try that.
     
Thread Status:
Not open for further replies.

Share This Page