Solved Getting item meta from item in a slot

Discussion in 'Plugin Development' started by plisov, Jan 15, 2017.

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

    plisov

    So I'm trying to create a traders plugin where if you put in items into a gui and press a button it will give you an item. However, these items can't be any old items. They must have a lore 1/5, 2/5, 3/5, 4/5 or 5/5. If they don't don't do anything. So basically I need to make it so that it will not do anything if the same exact item is placed into the gui for example two items that have 1/5 in their lore. This may sound confusing so if you have any other questions that you would like me to clarify, please let me know. This is the code I have so far.
    Code:
    package me.plisov.shards.trader;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.Arrays;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.DyeColor;
    import org.bukkit.Material;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class TraderGUI implements Listener {
        private static ItemStack glass, done;
    
        public static void createInv(Player player) {
            Inventory inv = Bukkit.getServer().createInventory(player, 27, ChatColor.RED + "Shards Trader");
    
            glass = glass(" ");
            done = done(ChatColor.GREEN + "" + ChatColor.BOLD + "Done");
    
            inv.setItem(0, glass);
            inv.setItem(1, glass);
            inv.setItem(2, glass);
            inv.setItem(3, glass);
            inv.setItem(4, glass);
            inv.setItem(5, glass);
            inv.setItem(6, glass);
            inv.setItem(7, glass);
            inv.setItem(8, glass);
            inv.setItem(9, glass);
            inv.setItem(10, glass);
            inv.setItem(16, glass);
            inv.setItem(17, glass);
            inv.setItem(18, glass);
            inv.setItem(19, glass);
            inv.setItem(20, glass);
            inv.setItem(21, glass);
            inv.setItem(22, glass);
            inv.setItem(23, glass);
            inv.setItem(24, glass);
            inv.setItem(25, glass);
            inv.setItem(26, done);
    
            player.openInventory(inv);
        }
    
        private static ItemStack glass(String name) {
            @SuppressWarnings("deprecation")
            ItemStack i = new ItemStack(Material.STAINED_GLASS_PANE, 1, DyeColor.WHITE.getWoolData());
            ItemMeta im = i.getItemMeta();
            im.setDisplayName(name);
            im.setLore(Arrays.asList(" "));
            i.setItemMeta(im);
            return i;
        }
    
        private static ItemStack done(String name) {
            @SuppressWarnings("deprecation")
            ItemStack i = new ItemStack(Material.STAINED_GLASS_PANE, 1, DyeColor.LIME.getWoolData());
            ItemMeta im = i.getItemMeta();
            im.setDisplayName(name);
            im.setLore(Arrays.asList(" "));
            i.setItemMeta(im);
            return i;
        }
    
        @EventHandler
        public void onInventoryClick(InventoryClickEvent e) throws IOException {
            Player player = (Player) e.getWhoClicked();
            FileConfiguration config = null;
            File file = new File("plugins" + File.separator + "Kingdom_Empire" + File.separator + "users" + File.separator + player.getUniqueId() + " (" + player.getName() + ")" + ".yml");
            config = YamlConfiguration.loadConfiguration(file);
    
            if(e.equals(null)) {
                e.setCancelled(true);
            }
            ItemStack hand = e.getCurrentItem();
            if(hand != null && hand.hasItemMeta()) {
                ItemMeta meta = hand.getItemMeta();
                if(e.getInventory().getTitle().equals(ChatColor.RED + "Shards Trader")) {
                    if(meta.hasDisplayName()) {
                        if(meta.getDisplayName().equals(" ")) {
                            e.setCancelled(true);
                        } else if(meta.getDisplayName().equals(ChatColor.GREEN + "" + ChatColor.BOLD + "Done")) {
                            e.setCancelled(true);
                            if(e.getInventory().firstEmpty() == -1) {
                                player.sendMessage("Working");
                            }
                        }
                    }
                }
            }
        }
    }
    Key Problems
    1. How do I get the item meta of an item that is in a slot
    2. How do I check if that item contains a lore 1/5 - 5/5
    3. How do I make sure the same item isn't placed in the GUI twice or more

    Clarifications
    1. The items placed in the GUI must be the same tier. Meaning if it's a Sword Shard, all the 5 item in the slots must have a display name of Sword Shard.
     
  2. Offline

    Zombie_Striker

    @plisov
    1. Inventory#getItem(slot)
    2. if lore.contains("X/5")
    3. Create a boolean array. it will contain 5 values, all set to false. Every time you find a number, set the value at that index you to true. If the value is already true, cancel whatever you are doing.
     
  3. Offline

    plisov

    Ah. So something like this?
    Code (open)

    package me.plisov.shards.trader;

    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Arrays;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.DyeColor;
    import org.bukkit.Material;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;

    public class TraderGUI implements Listener {
    private static ItemStack glass, done;

    public static ArrayList<Boolean> array = new ArrayList<Boolean>();

    public static void createInv(Player player) {
    Inventory inv = Bukkit.getServer().createInventory(player, 27, ChatColor.RED + "Shards Trader");

    glass = glass(" ");
    done = done(ChatColor.GREEN + "" + ChatColor.BOLD + "Done");

    inv.setItem(0, glass);
    inv.setItem(1, glass);
    inv.setItem(2, glass);
    inv.setItem(3, glass);
    inv.setItem(4, glass);
    inv.setItem(5, glass);
    inv.setItem(6, glass);
    inv.setItem(7, glass);
    inv.setItem(8, glass);
    inv.setItem(9, glass);
    inv.setItem(10, glass);
    inv.setItem(16, glass);
    inv.setItem(17, glass);
    inv.setItem(18, glass);
    inv.setItem(19, glass);
    inv.setItem(20, glass);
    inv.setItem(21, glass);
    inv.setItem(22, glass);
    inv.setItem(23, glass);
    inv.setItem(24, glass);
    inv.setItem(25, glass);
    inv.setItem(26, done);

    player.openInventory(inv);
    }

    private static ItemStack glass(String name) {
    @SuppressWarnings("deprecation")
    ItemStack i = new ItemStack(Material.STAINED_GLASS_PANE, 1, DyeColor.WHITE.getWoolData());
    ItemMeta im = i.getItemMeta();
    im.setDisplayName(name);
    im.setLore(Arrays.asList(" "));
    i.setItemMeta(im);
    return i;
    }

    private static ItemStack done(String name) {
    @SuppressWarnings("deprecation")
    ItemStack i = new ItemStack(Material.STAINED_GLASS_PANE, 1, DyeColor.LIME.getWoolData());
    ItemMeta im = i.getItemMeta();
    im.setDisplayName(name);
    im.setLore(Arrays.asList(" "));
    i.setItemMeta(im);
    return i;
    }

    @EventHandler
    public void onInventoryClick(InventoryClickEvent e) throws IOException {
    Player player = (Player) e.getWhoClicked();

    array.set(1, false);
    array.set(2, false);
    array.set(3, false);
    array.set(4, false);
    array.set(5, false);

    if(e.equals(null)) {
    e.setCancelled(true);
    }
    ItemStack hand = e.getCurrentItem();
    if(hand != null && hand.hasItemMeta()) {
    ItemMeta meta = hand.getItemMeta();
    if(e.getInventory().getTitle().equals(ChatColor.RED + "Shards Trader")) {
    if(meta.hasDisplayName()) {
    if(meta.getDisplayName().equals(" ")) {
    e.setCancelled(true);
    } else if(meta.getDisplayName().equals(ChatColor.GREEN + "" + ChatColor.BOLD + "Done")) {
    e.setCancelled(true);
    if(e.getInventory().getItem(11).getItemMeta().getLore().contains("1/5")) {
    if(array.get(1).equals(false)) {
    array.set(1, true);
    } else {
    e.setCancelled(true);
    }
    }
    if(e.getInventory().getItem(12).getItemMeta().getLore().contains("2/5")) {
    if(array.get(2).equals(false)) {
    array.set(2, true);
    } else {
    e.setCancelled(true);
    }
    }
    if(e.getInventory().getItem(13).getItemMeta().getLore().contains("3/5")) {
    if(array.get(3).equals(false)) {
    array.set(3, true);
    } else {
    e.setCancelled(true);
    }
    }
    if(e.getInventory().getItem(14).getItemMeta().getLore().contains("4/5")) {
    if(array.get(4).equals(false)) {
    array.set(4, true);
    } else {
    e.setCancelled(true);
    }
    }
    if(e.getInventory().getItem(15).getItemMeta().getLore().contains("5/5")) {
    if(array.get(5).equals(false)) {
    array.set(5, true);
    } else {
    e.setCancelled(true);
    }
    }
    }
    }
    }
    }
    }
    }
     
  4. Offline

    Zombie_Striker

    @plisov
    Do not copy and paste lines. Instead, either make methods or create for loops. In this case, create a for int loop, loop from 0 to 24, and set the item at that index equal to glass. That way, 25 lines can be reduced to 2.

    Also, don't abuse static. There is no reason for the arraylist or those items to be static. Remove those modifiers.

    All indexes should start at 0, not 1.

    Instead of checking specific slots, for loop through all the slots and check if each one contains a X/5 line of lore. Doing that will reduce roughly 20 lines.

    Main point: besides those things, that should work. Try it.
     
  5. Offline

    plisov

    Great advice, thanks. I've created a for loop for the glass and I've removed the static. However I don't understand how to for loop through all the slots and check if each one contains X/5 line of lore. How would I do so?

    Code (open)

    package me.plisov.shards.trader;

    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Arrays;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.DyeColor;
    import org.bukkit.Material;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;

    public class TraderGUI implements Listener {
    private static ItemStack glass, done;

    public ArrayList<Boolean> array = new ArrayList<Boolean>();

    public static void createInv(Player player) {
    Inventory inv = Bukkit.getServer().createInventory(player, 27, ChatColor.RED + "Shards Trader");

    glass = glass(" ");
    done = done(ChatColor.GREEN + "" + ChatColor.BOLD + "Done");

    for(int i = 0; i <= 10; i++) {
    inv.setItem(i, glass);
    }

    for(int i = 16; i <= 25; i++) {
    inv.setItem(i, glass);
    }

    inv.setItem(26, done);

    player.openInventory(inv);
    }

    private static ItemStack glass(String name) {
    @SuppressWarnings("deprecation")
    ItemStack i = new ItemStack(Material.STAINED_GLASS_PANE, 1, DyeColor.WHITE.getWoolData());
    ItemMeta im = i.getItemMeta();
    im.setDisplayName(name);
    im.setLore(Arrays.asList(" "));
    i.setItemMeta(im);
    return i;
    }

    private static ItemStack done(String name) {
    @SuppressWarnings("deprecation")
    ItemStack i = new ItemStack(Material.STAINED_GLASS_PANE, 1, DyeColor.LIME.getWoolData());
    ItemMeta im = i.getItemMeta();
    im.setDisplayName(name);
    im.setLore(Arrays.asList(" "));
    i.setItemMeta(im);
    return i;
    }

    @EventHandler
    public void onInventoryClick(InventoryClickEvent e) throws IOException {
    Player player = (Player) e.getWhoClicked();

    array.set(0, false);
    array.set(1, false);
    array.set(2, false);
    array.set(3, false);
    array.set(4, false);

    if(e.equals(null)) {
    e.setCancelled(true);
    }
    ItemStack hand = e.getCurrentItem();
    if(hand != null && hand.hasItemMeta()) {
    ItemMeta meta = hand.getItemMeta();
    if(e.getInventory().getTitle().equals(ChatColor.RED + "Shards Trader")) {
    if(meta.hasDisplayName()) {
    if(meta.getDisplayName().equals(" ")) {
    e.setCancelled(true);
    } else if(meta.getDisplayName().equals(ChatColor.GREEN + "" + ChatColor.BOLD + "Done")) {
    e.setCancelled(true);
    if(e.getInventory().getItem(11).getItemMeta().getLore().contains("1/5")) {
    if(array.get(0).equals(false)) {
    array.set(0, true);
    } else {
    e.setCancelled(true);
    }
    }
    if(e.getInventory().getItem(12).getItemMeta().getLore().contains("2/5")) {
    if(array.get(1).equals(false)) {
    array.set(1, true);
    } else {
    e.setCancelled(true);
    }
    }
    if(e.getInventory().getItem(13).getItemMeta().getLore().contains("3/5")) {
    if(array.get(2).equals(false)) {
    array.set(2, true);
    } else {
    e.setCancelled(true);
    }
    }
    if(e.getInventory().getItem(14).getItemMeta().getLore().contains("4/5")) {
    if(array.get(3).equals(false)) {
    array.set(3, true);
    } else {
    e.setCancelled(true);
    }
    }
    if(e.getInventory().getItem(15).getItemMeta().getLore().contains("5/5")) {
    if(array.get(4).equals(false)) {
    array.set(4, true);
    } else {
    e.setCancelled(true);
    }
    }
    }
    }
    }
    }
    }
    }
     
  6. Offline

    Zombie_Striker

    @plisov
    1. For Itemstack is : Inventory#getContents
    2. For loop though all the strings in is's lore.
    3. If that string contains /5 (do not check for the first number, just check for the /5)
    4. Use Integer.parseInt(loreLine.split("/")[0]) to get the first number before the 5. Check if the boolean at that index is equal to true. If so, do something/ If not, set it to true.
     
  7. Offline

    plisov

    I know for certain I'm doing this wrong. How do I loop through all the strings in the ItemStack's lore?

    Code:
                           
                            for(ItemStack is : e.getInventory().getContents()) {
                                for(ItemMeta im : im.getLore()) {
                                   
                                }
     
  8. Offline

    Zombie_Striker

    @plisov
    Change
    to
    Code:
    if(is!=null&&is.hasItemMeta()){
      for(String line: is.getItemMeta().getLore()){
        if(line.endsWith("/5")){
           if(array.get(Integer.parseInt(loreLine.split("/")[0]))){
             ///THIS ITEM HAS ALREADY BEEN ADDED
        }else{
          array.set(Integer.parseInt(loreLine.split("/")[0]),true);
        }
    }
     
  9. Offline

    plisov

    Ooh. Whelp, I was way off xP. What is loreLine? I'm not initializing it anywhere

    EDIT:
    Should I replace it with line?
     
  10. Offline

    Zombie_Striker

  11. Offline

    plisov

    In console I get an error saying that there's something wrong on line 70 which is array.set(0, false);
     
  12. Offline

    Zombie_Striker

    @plisov
    Can you post the full error log?
     
  13. Offline

    plisov

    Yup. Here yah go.
    Error (open)

    [01:00:28 ERROR]: Could not pass event InventoryClickEvent to Shards v1.0
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
    at net.minecraft.server.v1_11_R1.PlayerConnection.a(PlayerConnection.java:1839) [spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
    at net.minecraft.server.v1_11_R1.PacketPlayInWindowClick.a(SourceFile:33) [spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
    at net.minecraft.server.v1_11_R1.PacketPlayInWindowClick.a(SourceFile:10) [spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
    at net.minecraft.server.v1_11_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_101]
    at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_101]
    at net.minecraft.server.v1_11_R1.SystemUtils.a(SourceFile:46) [spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
    at net.minecraft.server.v1_11_R1.MinecraftServer.D(MinecraftServer.java:739) [spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
    at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:399) [spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
    at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:675) [spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
    at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:574) [spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_101]
    Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.ArrayList.rangeCheck(Unknown Source) ~[?:1.8.0_101]
    at java.util.ArrayList.set(Unknown Source) ~[?:1.8.0_101]
    at me.plisov.shards.trader.TraderGUI.onInventoryClick(TraderGUI.java:70) ~[?:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_101]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_101]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_101]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_101]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
    ... 15 more
     
  14. Offline

    Zombie_Striker

    @plisov
    It seems the list is empty. I thought you put in the falses.

    [Edit]
    1. You did not add the "false" values to the arraylist.
    2. You should not even be using arraysLists. Instead, you should use a boolean array. Use the following field instead:
    Code:
    public boolean[] array = {false,false,false,false,false}
    Then, replace the "set" line with
    Code:
    if(array[Integer.parseInt(loreLine.split("/")[0])]){
    
    }else{
    array[Integer.parseInt(loreLine.split("/")[0])] = true;
     
  15. Offline

    plisov

    Ok. Sorry about that. It seems like it got rid of the previous error however now it's giving me a new error
    Error (open)

    [01:17:23 ERROR]: Could not pass event InventoryClickEvent to Shards v1.0
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
    at net.minecraft.server.v1_11_R1.PlayerConnection.a(PlayerConnection.java:1839) [spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
    at net.minecraft.server.v1_11_R1.PacketPlayInWindowClick.a(SourceFile:33) [spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
    at net.minecraft.server.v1_11_R1.PacketPlayInWindowClick.a(SourceFile:10) [spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
    at net.minecraft.server.v1_11_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_101]
    at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_101]
    at net.minecraft.server.v1_11_R1.SystemUtils.a(SourceFile:46) [spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
    at net.minecraft.server.v1_11_R1.MinecraftServer.D(MinecraftServer.java:739) [spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
    at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:399) [spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
    at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:675) [spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
    at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:574) [spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_101]
    Caused by: java.lang.NumberFormatException: For input string: "ยบ71"
    at java.lang.NumberFormatException.forInputString(Unknown Source) ~[?:1.8.0_101]
    at java.lang.Integer.parseInt(Unknown Source) ~[?:1.8.0_101]
    at java.lang.Integer.parseInt(Unknown Source) ~[?:1.8.0_101]
    at me.plisov.shards.trader.TraderGUI.onInventoryClick(TraderGUI.java:84) ~[?:?]
    at sun.reflect.GeneratedMethodAccessor9.invoke(Unknown Source) ~[?:?]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_101]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_101]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
    ... 15 more


    on line 84 which is this line

    Code:
                                            if(array[Integer.parseInt(line.split("/")[0])]){
    
     
  16. Offline

    Zombie_Striker

    @plisov
    Did you put chatcolors on that line? If so, you will need to strip the chatcolors. Use
    Code:
    Integer.parseInt(ChatColor.stripColor(line.split("/")[0]))
     
  17. Offline

    plisov

    Indeed I do. That seemed to have fixed that error however when I fill up the trader gui completely, it gives me the same error on the same line. (Or at least it looks like the same error except this time it says ArrayIndexOutOfBoundsException: 5)
    Error (open)

    [01:29:16 ERROR]: Could not pass event InventoryClickEvent to Shards v1.0
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
    at net.minecraft.server.v1_11_R1.PlayerConnection.a(PlayerConnection.java:1839) [spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
    at net.minecraft.server.v1_11_R1.PacketPlayInWindowClick.a(SourceFile:33) [spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
    at net.minecraft.server.v1_11_R1.PacketPlayInWindowClick.a(SourceFile:10) [spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
    at net.minecraft.server.v1_11_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_101]
    at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_101]
    at net.minecraft.server.v1_11_R1.SystemUtils.a(SourceFile:46) [spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
    at net.minecraft.server.v1_11_R1.MinecraftServer.D(MinecraftServer.java:739) [spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
    at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:399) [spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
    at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:675) [spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
    at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:574) [spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_101]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 5
    at me.plisov.shards.trader.TraderGUI.onInventoryClick(TraderGUI.java:84) ~[?:?]
    at sun.reflect.GeneratedMethodAccessor21.invoke(Unknown Source) ~[?:?]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_101]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_101]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
    ... 15 more


    [​IMG]
     
  18. Online

    timtower Ninja on the waves Moderator

    @plisov Could you post your updated code?
    Could you also use code blocks instead of spoiler for your errors?
     
  19. Offline

    plisov

    Sure thing.
    Code:
    
    
     
    Last edited: Jan 16, 2017
  20. Online

    timtower Ninja on the waves Moderator

    @plisov Integer.parseInt(stuff)-1
    Now it goes from 1 to 5, java starts counting at 0
     
  21. Offline

    plisov

    So I should do this instead?
    if(array[Integer.parseInt(ChatColor.stripColor(line.split("/")[0]))-1]) {
     
  22. Online

    timtower Ninja on the waves Moderator

  23. Offline

    plisov

    It now says there is a problem on line 87
    array[Integer.parseInt(ChatColor.stripColor(line.split("/")[0]))] = true;
    If I try adding -1 to it, it turns red.
     
  24. Online

    timtower Ninja on the waves Moderator

    @plisov That should work though.
    Does it also tell what the error is?
    And you can always put the result in a temporary variable as well.
     
  25. Offline

    plisov

    Well, this is the error message. It says it's out of bounds
    Code:
    [01:45:46 ERROR]: Could not pass event InventoryClickEvent to Shards v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
            at net.minecraft.server.v1_11_R1.PlayerConnection.a(PlayerConnection.java:1839) [spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
            at net.minecraft.server.v1_11_R1.PacketPlayInWindowClick.a(SourceFile:33) [spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
            at net.minecraft.server.v1_11_R1.PacketPlayInWindowClick.a(SourceFile:10) [spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
            at net.minecraft.server.v1_11_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_101]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_101]
            at net.minecraft.server.v1_11_R1.SystemUtils.a(SourceFile:46) [spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
            at net.minecraft.server.v1_11_R1.MinecraftServer.D(MinecraftServer.java:739) [spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
            at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:399) [spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
            at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:675) [spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
            at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:574) [spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_101]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 5
            at me.plisov.shards.trader.TraderGUI.onInventoryClick(TraderGUI.java:87) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_101]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_101]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_101]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_101]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot-1.11.2.jar:git-Spigot-7d78b81-d5e7885]
            ... 15 more
     
  26. Online

    timtower Ninja on the waves Moderator

    @plisov Then you are still using the parsed int, not the parsed int -1
     
  27. Offline

    JanTuck

    Try surrounding it in parenthesis.
     
  28. Offline

    plisov

    @Zombie_Striker @timtower @JanTuck
    I fixed the problem. I was putting it after the wrong bracket xP. That got rid of all the errors. If I'm not mistaking, this will check if each item has a X/5 correct?
    Updated Code
    Code:
    
    
    How would I make it so,if each item is a different item with X/5 in it's lore and not a repeat of another item, it'll do something, else, cancel what it's doing. Also, if they place for example 4/5 sword shards and then place an ax shard, have it also cancel what it's doing. So only when the items have X/5 in their lore and the same display name will it do something.
     
    Last edited: Jan 16, 2017
  29. Offline

    JanTuck

    Try to figure it out xD I believe in you.

    Sendt fra min ALE-L21 med Tapatalk
     
  30. Offline

    plisov

    @Zombie_Striker @timtower @JanTuck
    I have another question. I think I found a completely different way of doing this. This is how I'm doing it currently.
    Code:
    package me.plisov.shards.trader;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Arrays;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.DyeColor;
    import org.bukkit.Material;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class TraderGUI implements Listener {
        private static ItemStack glass, done;
    
        public boolean oneSword = true;
    
        public boolean[] array = {false,false,false,false,false};
    
        public static void createInv(Player player) {
            Inventory inv = Bukkit.getServer().createInventory(player, 27, ChatColor.RED + "Shards Trader");
    
            glass = glass(" ");
            done = done(ChatColor.GREEN + "" + ChatColor.BOLD + "Done");
    
            for(int i = 0; i <= 10; i++) {
                inv.setItem(i, glass);
            }
    
            for(int i = 16; i <= 25; i++) {
                inv.setItem(i, glass);
            }
    
            inv.setItem(26, done);
    
            player.openInventory(inv);
        }
    
        private static ItemStack glass(String name) {
            @SuppressWarnings("deprecation")
            ItemStack i = new ItemStack(Material.STAINED_GLASS_PANE, 1, DyeColor.WHITE.getWoolData());
            ItemMeta im = i.getItemMeta();
            im.setDisplayName(name);
            im.setLore(Arrays.asList(" "));
            i.setItemMeta(im);
            return i;
        }
    
        private static ItemStack done(String name) {
            @SuppressWarnings("deprecation")
            ItemStack i = new ItemStack(Material.STAINED_GLASS_PANE, 1, DyeColor.LIME.getWoolData());
            ItemMeta im = i.getItemMeta();
            im.setDisplayName(name);
            im.setLore(Arrays.asList(" "));
            i.setItemMeta(im);
            return i;
        }
    
        @EventHandler
        public void onInventoryClick(InventoryClickEvent e) throws IOException {
            Player player = (Player) e.getWhoClicked();
    
            oneSword = true;
    
            ItemStack hand = e.getCurrentItem();
            if(hand != null && hand.hasItemMeta()) {
                ItemMeta meta = hand.getItemMeta();
                if(e.getInventory().getTitle().equals(ChatColor.RED + "Shards Trader")) {
                    if(meta.hasDisplayName()) {
                        if(meta.getDisplayName().equals(" ")) {
                            e.setCancelled(true);
                        } else if(meta.getDisplayName().equals(ChatColor.GREEN + "" + ChatColor.BOLD + "Done")) {
                            e.setCancelled(true);
                            for(ItemStack is : e.getInventory().getContents()) {
                                if(is != null && is.hasItemMeta()){
                                    for(String line : is.getItemMeta().getLore()) {
                                        if(line.endsWith("/5")) {
                                            if(e.getInventory().firstEmpty() == -1) {
                                                if(oneSword == Boolean.TRUE) {
                                                    ItemStack is1 = new ItemStack(Material.DIAMOND_SWORD);
                                                    ItemMeta is1meta = is1.getItemMeta();
    
                                                    is1meta.setDisplayName(ChatColor.RED + "" + ChatColor.BOLD + "Artifact Sword");
                                                    is1meta.addEnchant(Enchantment.DAMAGE_ALL, 8, true);
                                                    is1meta.addEnchant(Enchantment.DURABILITY, 8, true);
                                                    is1meta.addEnchant(Enchantment.DAMAGE_UNDEAD, 7, true);
                                                    is1meta.addEnchant(Enchantment.DAMAGE_ARTHROPODS, 7, true);
                                                    is1meta.addEnchant(Enchantment.LOOT_BONUS_MOBS, 7, true);
    
                                                    is1.setItemMeta(is1meta);
                                                    oneSword = false;
                                                    player.getInventory().addItem(is1);
                                                } else {
    
                                                }
                                            }
                                            e.getWhoClicked().closeInventory();
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    It doesn't work when you use regular blocks which is good
    However if you put one of the shards with /5 as their lore in the gui and fill the rest with a block, it'll still give you the item. So I'm wondering how to make sure that there are 5 items in the gui that contain /5 in their lore. How would I do so?
     
Thread Status:
Not open for further replies.

Share This Page