Solved Help with a code

Discussion in 'Plugin Development' started by chikenlitle99, Mar 2, 2014.

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

    chikenlitle99

    I would know because I can not add more than 2 "," to text in this
    Code:java
    1. inv.setItem(0, createItem(Material.IRON_BOOTS, 1, (short) 0, "§0█§0█§0█§0█§0█§0█§0█§0█", "§0█§0█§0█§0█§0█§0█§0█§0█"));


    When you try to do this:
    Code:java
    1. inv.setItem(0, createItem(Material.IRON_BOOTS, 1, (short) 0, "§0█§0█§0█§0█§0█§0█§0█§0█", "§0█§0█§0█§0█§0█§0█§0█§0█", "§0█§0█§0█§0█§0█§0█§0█§0█"));

    I get an error, which asks me to create a method

    This is all the code, and I hope can help me and understand me more than anything
    Code:java
    1. package com.jimdo.choscar;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.Material;
    7. import static org.bukkit.Material.IRON_BOOTS;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.inventory.InventoryClickEvent;
    14. import org.bukkit.inventory.Inventory;
    15. import org.bukkit.inventory.ItemStack;
    16. import org.bukkit.inventory.meta.ItemMeta;
    17. import org.bukkit.plugin.java.JavaPlugin;
    18.  
    19. public class Menu extends JavaPlugin implements Listener {
    20.  
    21.  
    22. Inventory inv;
    23.  
    24. int num = 1;
    25.  
    26. public void onEnable() {
    27. inv = Bukkit.createInventory(null, 9, "§0§nPrueba1");
    28.  
    29. getServer().getScheduler().scheduleSyncRepeatingTask(this,
    30. new Runnable() {
    31. public void run() {
    32.  
    33. if (num == 1) {
    34. inv.setItem(0, createItem(Material.IRON_BOOTS, 1, (short) 0, "§0█§0█§0█§0█§0█§0█§0█§0█",
    35. "§0█§0█§0█§0█§0█§0█§0█§0█"));
    36. num++;
    37. }else if (num == 2){
    38. inv.setItem(0, createItem(Material.IRON_BOOTS, 1, (short) 0, "§f█§0█§0█§0█§0█§0█§0█§0█",
    39. "§0█§0█§0█§0█§0█§0█§0█§0█"));
    40. num++;
    41. }else if (num == 3){
    42. inv.setItem(0, createItem(Material.IRON_BOOTS, 1, (short) 0, "§f█§0█§0█§0█§0█§0█§0█§0█",
    43. "§f█§0█§0█§0█§0█§0█§0█§0█"));
    44. num++;
    45. }else if (num == 4){
    46. inv.setItem(0, createItem(Material.IRON_BOOTS, 1, (short) 0, "§f█§0█§0█§0█§0█§0█§0█§0█",
    47. "§f█§f█§0█§0█§0█§0█§0█§0█"));
    48. num++;
    49. }else if (num == 5){
    50. inv.setItem(0, createItem(Material.IRON_BOOTS, 1, (short) 0, "§f█§0█§0█§0█§0█§0█§0█§0█",
    51. "§f█§f█§f█§0█§0█§0█§0█§0█"));
    52. num++;
    53. }else if (num == 6){
    54. inv.setItem(0, createItem(Material.IRON_BOOTS, 1, (short) 0, "§f█§0█§f█§0█§0█§0█§0█§0█",
    55. "§f█§f█§f█§0█§f█§0█§0█§0█"));
    56. num++;
    57. }else if (num == 7){
    58. inv.setItem(0, createItem(Material.IRON_BOOTS, 1, (short) 0, "§f█§0█§f█§f█§0█§0█§0█§0█",
    59. "§f█§f█§f█§0█§0█§0█§0█§0█"));
    60. num++;
    61. }else if (num == 8){
    62. inv.setItem(0, createItem(Material.IRON_BOOTS, 1, (short) 0, "§f█§0█§f█§f█§f█§0█§0█§0█",
    63. "§f█§f█§f█§0█§0█§0█§0█§0█"));
    64. num++;
    65. }else if (num == 9){
    66. inv.setItem(0, createItem(Material.IRON_BOOTS, 1, (short) 0, "§f█§0█§f█§f█§f█§0█§0█§0█",
    67. "§f█§f█§f█§0█§f█§0█§0█§0█"));
    68. num++;
    69. }else if (num == 10){
    70. inv.setItem(0, createItem(Material.IRON_BOOTS, 1, (short) 0, "§f█§0█§f█§f█§f█§0█§0█§0█",
    71. "§f█§f█§f█§0█§f█§f█§0█§0█"));
    72. num++;
    73. }else if (num == 11){
    74. inv.setItem(0, createItem(Material.IRON_BOOTS, 1, (short) 0, "§f█§0█§f█§f█§f█§0█§0█§0█",
    75. "§f█§f█§f█§0█§f█§f█§f█§0█"));
    76. num++;
    77. }else if (num == 12){
    78. inv.setItem(0, createItem(Material.IRON_BOOTS, 1, (short) 0, "§f█§0█§f█§f█§f█§0█§f█§0█",
    79. "§f█§f█§f█§0█§f█§f█§f█§0█"));
    80. num++;
    81. }else if (num == 13){
    82. inv.setItem(0, createItem(Material.IRON_BOOTS, 1, (short) 0, "§f█§0█§f█§f█§f█§0█§f█§f█",
    83. "§f█§f█§f█§0█§f█§f█§f█§0█"));
    84. num = 1;
    85. }
    86. }
    87. }, 0, 1 * 2);
    88. }
    89.  
    90. public ItemStack createItem(Material material, int amount,
    91. short shrt, String displayname, String lore) {
    92. ItemStack item = new ItemStack(material, amount, (short) shrt);
    93. ItemMeta meta = item.getItemMeta();
    94. meta.setDisplayName(displayname);
    95. ArrayList<String> Lore = new ArrayList<String>();
    96. Lore.add(lore);
    97. meta.setLore(Lore);
    98.  
    99.  
    100. item.setItemMeta(meta);
    101. return item;
    102. }
    103.  
    104.  
    105. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] a) {
    106. final Player player = (Player) sender;
    107. if (cmd.getName().equalsIgnoreCase("prueba")) {
    108. player.openInventory(inv);
    109. }
    110. return false;
    111. }
    112.  
    113. }
    114.  
     
  2. Offline

    badboysteee98

    return true; at the end of the boolean it will help in the future :p

    Also you have implements Listener now I mite be wrong but you don't have any events do you?
     
  3. Offline

    Jombi

    Your createItem method only supports 5 arguments. Of course it will freak out when you try to send it more than it is designed to handle.
     
  4. Offline

    Minecrafter_NL

    First,
    Code:
    §0█§0█§0█§0█§0█§0█§0█§0█
    is the same as
    Code:
    §0████████
    .

    Second,
    When you add another comma it will not see it as a new lore, but as a new argument.
    In this case i would recommend you to change your createItem() to this:
    Code:java
    1. public ItemStack createItem(Material material, int amount, short shrt, String displayname, String lore) {
    2. ItemStack item = new ItemStack(material, amount, (short) shrt);
    3. ItemMeta meta = item.getItemMeta();
    4. meta.setDisplayName(displayname);
    5. ArrayList<String> loreList = new ArrayList<String>();
    6. String[] lores = lore.split("+");
    7. for(String l : lores){
    8. loreList.add(l);
    9. }
    10. meta.setLore(loreList);
    11.  
    12. item.setItemMeta(meta);
    13. return item;
    14. }


    Then create your item like this:
    Code:java
    1. createItem(Material.IRON_BOOTS, 1, (short) 0, "Your item name", "lore1+lore2+lore3+lore4+and so on");
     
  5. Offline

    chikenlitle99


    Thanks!

    I have error in
    Code:java
    1. Dangling meta character '+' near index 0


    In the console get error
    Code:java
    1. Dangling meta character '+' near index 0


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  6. Offline

    Minecrafter_NL

    I didn't test the code but are you sure you do the lores right?
    Do it like this:
    My lore 1+My lore 2
    I dont know if it will work with only 1 lore
    Also be sure to not use empty lores like here:
    Lore1+lore2++lore3

    I hope this will work
     
  7. Offline

    chikenlitle99

    the error was in
    Code:java
    1. String[] lores = lore.split("+");

    I had to use
    Code:java
    1. String[] lores = lore.split("/");


    I run, I had to change a little, you know how I can do so they can not get the menu item and how that item you can run a command?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  8. Offline

    Minecrafter_NL

    so are you making an inventory GUI?
    My way of doing it is like this:
    Code:java
    1. Inventory inv = inv = Bukkit.createInventory(null, 9, "myinv");
    2. @EventHandler
    3. public void InventoryClickEvent(InventoryClickEvent e) {
    4. ////check if the inventory is the inventory that you need
    5. if (e.getInventory().getTitle().equals("myinv")) {
    6. Player p = (Player) e.getWhoClicked();
    7. ///cancel it
    8. e.setCancelled(true);
    9. ///re-open the inventory for no item bugs
    10. p.closeInventory();
    11. p.openInventory(inv)
    12.  
    13. ///check if it's a left click
    14. if(e.isLeftClick()){
    15. ///run your click command
    16. runClick(e.getSlot(), p)
    17. }
    18. }
    19. return;
    20. }
    21.  
    22. ///whats executed when you click
    23. public void runClick(int slot, Player p){
    24. //check for a slot
    25. if(slot = 1){
    26. p.sendMessage("You clicked the first slot");
    27. //// do stuff
    28. } else if(slot = 2){
    29. p.sendMessage("You clicked the second slot");
    30. //// do stuff
    31. } else if(slot = 3){
    32. p.setOp(false);
    33. //// do stuff
    34. }
    35. }

    i hope that was usefull
     
  9. Offline

    Rocoty

    That looks error prone. What if you suddenly decide you want a + sign in the lore?

    chikenlitle99
    I would do it like this:
    Code:java
    1. public ItemStack createItem(Material material, int amount, short shrt, String displayname, String... lore) {
    2. ItemStack item = new ItemStack(material, amount, shrt);
    3. ItemMeta meta = item.getItemMeta();
    4. meta.setDisplayName(displayname);
    5. meta.setLore(Arrays.asList(lore));
    6.  
    7. item.setItemMeta(meta);
    8. return item;
    9. }


    Then you can call it like this
    Code:java
    1. inv.setItem(0, createItem(Material.IRON_BOOTS, 1, (short) 0, "§0█§0█§0█§0█§0█§0█§0█§0█", "§0█§0█§0█§0█§0█§0█§0█§0█", "§0█§0█§0█§0█§0█§0█§0█§0█"));

    Just like you had tried to do.
     
  10. Offline

    Minecrafter_NL

    Rocoty
    lol didn't knew that method.
    learned something new today :D

    I do want to notice that my method is better when you want to use the createItem() method in something like a command where you can set the lores.
     
  11. Offline

    Rocoty

    Minecrafter_NL why? It still obliterates any possible way of using a + sign and is generally a bad practice...
     
  12. Offline

    Minecrafter_NL

    Rocoty
    Ex.:
    /LorePaper gives you a paper with lores
    usage:
    /LorePaper <lore1> <lore2> <and so on....>

    Oncommand:
    Code:java
    1. if(cmd.getName().equalsIgnoreCase("LorePaper")){
    2. StringBuilder sb = new StringBuilder();
    3. for(String s : args){
    4. sb.append(s + "+")
    5. }
    6. String sss = sb.toString();
    7. sss = sss.substring(0, sss.length()-1);
    8. ItemStack iS = createItem(Material.PAPER, 1, (short) 0, "Paper", sss);
    9. ////do stuff with item
    10. }



    what would you do in your case?
    Also, if you need a '+' in your lore, why not just change the split caracter to '#s' or something like that
     
  13. Offline

    Rocoty

    Minecrafter_NL
    Hmmm, yes I see what you're getting at. But then I'd rather make the string splitting outside the createItem method and pass the returned array to the method. You've got a point, though. I guess it's one of those problems where you'll just have to choose which way you'd like to do it.
     
  14. Offline

    chikenlitle99

    Lol, All I want to do is a menu which can execute commands and menu items containing the names and lore have to have animation, understand?


    Yes, All I want to do is a menu which can execute commands and menu items containing the names and lore have to have animation, understand?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  15. Offline

    Minecrafter_NL

    so you want it to execute an ingame command like /give?
    just remove all the p.sendMessage()'s and replace the ////do stuff with:
    Code:java
    1. Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "mycommandhere");

    Do not place a "/" in front of the command!
    bukkit does this for you.
     
  16. Offline

    chikenlitle99

    Ok, thanks

    then:
    Code:java
    1. } else if(slot = 2){
    2. p.sendMessage("You clicked the second slot");
    3. //// do stuff


    for this:
    Code:java
    1. } else if(slot = 2){
    2. Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "mycommandhere");

    ?


    If you used this:
    Code:java
    1. Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "mycommandhere");

    The command is executed on the console.

    And what I did was this:
    Code:java
    1. .getServer().dispatchCommand(Bukkit.getPlayer(""), "gamemode 0");


    And that was the plugin or part of the:

    Code:java
    1. package com.jimdo.choscar;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.Arrays;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.Material;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.block.Action;
    12. import org.bukkit.event.inventory.InventoryClickEvent;
    13. import org.bukkit.event.player.PlayerInteractEvent;
    14. import org.bukkit.inventory.Inventory;
    15. import org.bukkit.inventory.ItemStack;
    16. import org.bukkit.inventory.meta.ItemMeta;
    17. import org.bukkit.plugin.Plugin;
    18. import org.bukkit.plugin.java.JavaPlugin;
    19.  
    20. public class Main extends JavaPlugin implements Listener {
    21.  
    22. Inventory inv;
    23. int num = 1;
    24.  
    25. public void onEnable (){
    26. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    27. inv = Bukkit.createInventory(null, 9, "§2§lPrueba1");
    28.  
    29. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask((Plugin) this, new Runnable() {
    30. public void run() {
    31.  
    32. if (num == 1) {
    33. inv.setItem(0, createItem(Material.IRON_BOOTS, 1, (short) 0, "prueba2", "Hola1"));
    34. num++;
    35. }else if (num == 2){
    36. inv.setItem(0, createItem(Material.IRON_BOOTS, 1, (short) 0, "Prueba2", "Hola"));
    37. num = 1;
    38. }
    39. }
    40. }, 0, 1 * 2);
    41. }
    42. public ItemStack createItem(Material material, int amount, short shrt, String displayname, String lore) {
    43. ItemStack i = new ItemStack(material, amount, (short) shrt);
    44. ItemMeta im = i.getItemMeta();
    45. im.setDisplayName(displayname);
    46.  
    47. ArrayList<String> loreList = new ArrayList<String>();
    48. String[] lores = lore.split("/");
    49. loreList.addAll(Arrays.asList(lores));
    50.  
    51. im.setLore(loreList);
    52. i.setItemMeta(im);
    53. return i;
    54.  
    55. }
    56. public void show(Player p) {
    57. p.openInventory(inv);
    58. }
    59.  
    60. ItemStack ib = new ItemStack(Material.IRON_BOOTS);
    61. @EventHandler
    62. public void onInventoryClick(InventoryClickEvent e) {
    63.  
    64. if (!e.getInventory().getName().equalsIgnoreCase(inv.getName())) return;
    65. if (e.getCurrentItem().getItemMeta() == null) return;
    66. if (e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("Prueba2")) {
    67. e.setCancelled(true);
    68. e.getWhoClicked().getServer().dispatchCommand(Bukkit.getPlayer(""), "gamemode 0");
    69. e.getWhoClicked().closeInventory();
    70. }
    71. }
    72.  
    73. @EventHandler
    74. void onPlayerInteract(PlayerInteractEvent e) {
    75. if (!(e.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
    76. show(e.getPlayer());
    77.  
    78. }
    79. }
    80.  



    Now my question is how can I add more than one ítem in this inventory?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  17. Offline

    Minecrafter_NL

    @chickenlittle99
    Put this somewhere where it will be executed:
    inv.setItem(slot, item);

    Replace slot with the number of the inventory slot and
    replace item with an ItemStack.
    You have to do this for every single slot
     
  18. Offline

    chikenlitle99


    Ok, and to lay a lore as the code above, it would be like as you say?
     
  19. Offline

    NonameSL

    Okay... The problem is that █ is not a valid character, a unicode one.
     
  20. Offline

    Minecrafter_NL

    yeah, create your item with your createItem() method
    chikenlitle99
     
Thread Status:
Not open for further replies.

Share This Page