Solved Itemlore next Line?

Discussion in 'Plugin Development' started by PreFiXAUT, Aug 17, 2013.

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

    PreFiXAUT

    Hey, I was trying to add a Lore to an Item, but it won't set Lines, so basicly the entire Text is on one Line and looks damn:
    Show Spoiler
    [​IMG]

    My Code:
    Show Spoiler
    Code:java
    1. ItemStack eye = new ItemStack(Material.EYE_OF_ENDER);
    2. ItemMeta eyemeta = eye.getItemMeta();
    3. eyemeta.setDisplayName(this.plugin.getConfig().getString("items.eye_of_ender.name"));
    4. int t = 0;
    5. for (String output : plugin.getConfig().getStringList("items.eye_of_ender.lore"))
    6. {
    7. List<String> lore = new ArrayList<String>();
    8. lore.add(t, output);
    9. eyemeta.setLore(lore);
    10. t ++;
    11. }
    12. eye.setItemMeta(eyemeta);
    13. p.getInventory().addItem(eye);


    Can somebody help?
     
  2. Offline

    Lactem

    This is actually pretty simple. Put "\n" where you want to have it start a new line. That's not.the usual slash for commands like /spawn. It's the other one. Example: "Teleport you into the premium lobby \n when you're premium."
     
  3. Offline

    PreFiXAUT

    Lactem
    I tried to, but it won't work. I saved the String in a config, maybe thats the Error?
     
  4. Offline

    Lactem

    Try just doing this, just for debugging purposes. Don't get it from the config and see if it works.
     
  5. Offline

    dunbaratu

    Lore is an array of strings, each shown on a line. Just make different array elements for each line, like so:
    instead of:
    Lore.add( t, "line one\nline two" );
    do this:
    Lore.add( t, "line one" );
    Lore.add( ++t, "line two" );
     
  6. Offline

    Lactem

    You could do that, too.
     
  7. Offline

    PreFiXAUT

    dunbaratu
    Thanks that's working now :)
    Is it possible to splitt an String when a special char/string is in there like:
    new String test = ("Text1 \n Text2");
    When yes, how does it work?
     
  8. Offline

    dunbaratu

    Yes. It's called "split". It's a standard Java String method, documented in the Java platform javadoc.

    Use it like this:
    Code:
    String srcString = "Text1\nText2\nText3";
    new String[] lines = srcString.split( "\n" );
    // At this point:
    //  lines[0] = "Text1";
    //  lines[1] = "Text2";
    //  lines[2] = "Text3";
    
    Warning: The split() method interprets the parameter as a regular expression, not just a raw string, so some characters will have special meaning and behave strangely if you don't understand what a regular expression is. Luckily, "\n" isn't one of them so you can just use it normally.
     
  9. Offline

    PreFiXAUT

    dunbaratu
    Thanks works nice, but I still got a problem.
    I wan't to loop throgh the eintire List and Output it, but I don't know how.
    I tried to do it by myself :
    Show Spoiler
    Code:java
    1. List<String> lore = new ArrayList<String>();
    2. String[] string_message = (plugin.getConfig().getString("items.eye_of_ender.lore")).split(" \n ");
    3. int i = 0;
    4. for (String output : string_message)
    5. {
    6. lore.add(i, string_message[1]);
    7. i ++;
    8. }

    But this will give me some Errors:
    Show Spoiler
    Code:
    22:29:05 [SEVERE] Could not pass event PlayerJoinEvent to PVP Lobbys v0.2
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
            at net.minecraft.server.v1_6_R2.PlayerList.c(PlayerList.java:206)
            at net.minecraft.server.v1_6_R2.PlayerList.a(PlayerList.java:102)
            at net.minecraft.server.v1_6_R2.PendingConnection.e(PendingConnection.java:130)
            at net.minecraft.server.v1_6_R2.PendingConnection.d(PendingConnection.java:43)
            at net.minecraft.server.v1_6_R2.DedicatedServerConnectionThread.a(DedicatedServerConnectionT
    hread.java:41)
            at net.minecraft.server.v1_6_R2.DedicatedServerConnection.b(SourceFile:29)
            at net.minecraft.server.v1_6_R2.MinecraftServer.t(MinecraftServer.java:590)
            at net.minecraft.server.v1_6_R2.DedicatedServer.t(DedicatedServer.java:226)
            at net.minecraft.server.v1_6_R2.MinecraftServer.s(MinecraftServer.java:486)
            at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java:419)
            at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:582)
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
            at at.prefixaut.pvp_plugin.listener.JoinQuitListener.onJoin(JoinQuitListener.java:102)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
            at java.lang.reflect.Method.invoke(Unknown Source)
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
            ... 14 more

    I think it's cause tha String output :/
     
  10. Offline

    dunbaratu

    Notice this line in the stack trace:

    Caused by: java.lang.ArrayIndexOutOfBoundsException: 1

    I think that's caused by this line in your code:

    lore.add(i, string_message[1]);

    Notice you say "string_message[1]". Notice you use the digit "1" and not a variable there.
    That's hardcoded to get the second thing in the string_message array no matter what, and is utterly ignoring the loop variable you set up in the for loop header.

    Change 'string_message[1]' to 'output', since 'output' is the name of the variable you're using in your for loop.
     
    PreFiXAUT likes this.
  11. Offline

    NLGamingBross

    I have used lore's only by new im.

    Here is what i used for mine plugin "Minetrailshide"
    Code:java
    1. im.setLore(Arrays.asList(new String[] { ChatColor.DARK_PURPLE + "LeftClick - Hide" , ChatColor.DARK_PURPLE + "RightClick - Show" }));
     
  12. Offline

    PreFiXAUT

Thread Status:
Not open for further replies.

Share This Page