Lore to Strings Conversion Problem

Discussion in 'Plugin Development' started by MCJoshua345, Jan 12, 2016.

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

    MCJoshua345

    Hello. Today I have a error. I was trying to develop a plugin modeled after AuctionHouse, (because it is $10.00 USD, and I don't want to pay that.) and every was working semi-smoothly. Just a few bugs I could squash. But there is this one problem. Here is the code:
    Code:
    public static void buyItem(ItemStack item, Player p){
            List<String> lore = item.getItemMeta().getLore();
            String loreowner = lore.get(1);
            String loreprice = lore.get(2);
            String[] owner_words = loreowner.split("\\W+");
            String[] price_words = loreprice.split("\\W+");
            List<String> ownerlist = Arrays.asList(owner_words);
            List<String> pricelist = Arrays.asList(price_words);
            String owner = ownerlist.get(2);
            String price = pricelist.get(2);
            if(owner.equals(p.getName())){
                if(return_list.contains(p)){
                    return_list.remove(p);
                    shopInventory.removeItem(item);
                    List<String> newLore = new ArrayList<String>();
                    item.getItemMeta().setLore(newLore);
                    p.getInventory().addItem(item);
                    p.sendMessage(StringStorage.at + ChatColor.RED + "You took back this item.");
                }else if(!return_list.contains(p)){
                    return_list.add(p);
                    p.sendMessage(StringStorage.at + ChatColor.GREEN + "If you really want to take back this item, click it again. If not, exit the Trades Inventory.");
                }
            }else{
                if(confirm_list.contains(p)){
                    confirm_list.remove(p);
                    Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "eco give " + owner + " " + price);
                    Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "eco take " + p.getName() + " " + price);
                    shopInventory.removeItem(item);
                    List<String> newLore = new ArrayList<String>();
                    item.getItemMeta().setLore(newLore);
                    p.getInventory().addItem(item);
                    p.sendMessage(StringStorage.at + ChatColor.GREEN + "You bought " + ChatColor.RESET + item.getItemMeta().getDisplayName() + ChatColor.GREEN + " for " + ChatColor.AQUA + "$" + price + ChatColor.GREEN + " from " + ChatColor.AQUA + owner + ChatColor.GREEN + "!");
                }else if(!confirm_list.contains(p)){
                    p.sendMessage(StringStorage.at + ChatColor.GREEN + "If you really want to buy this item, click it again. If not, exit the Trades Inventory.");
                    confirm_list.add(p);
                }
            }
        }
    The problem is that the variables owner & price return the owner and the price, with a "b" at the start. If someone could tell me what the problem is and how to fix it, it would be very appreciated.
    - Thank You, AlphaGladiator
     
  2. Offline

    Zombie_Striker

    @MCJoshua345
    Can we see what the ownerlist/pricelist is equal to? Cane we see what "loreOwner" and "lorePrice" is equal to? If the lore contains a chatcolor, you may need to remove all chatcolors.
     
  3. @MCJoshua345
    You should be more detailed, not just paste your code. Could you show us the item's lore?
     
  4. Offline

    87pen

    @MCJoshua345 Unless in a far away land that I hope exists, you check if the item has the meta you are getting, all I see is NPEs. I believe your problem is you never set your items Metadata?
     
  5. Offline

    MCJoshua345

    Code:
    public static void postItem(ItemStack item, Player p, int price){
            ItemMeta newMeta = item.getItemMeta();
            List<String> lore = new ArrayList<String>();
            lore.add(ChatColor.GRAY + "---------------");
            lore.add(ChatColor.YELLOW + "Owner: " + ChatColor.AQUA + p.getName());
            lore.add(ChatColor.YELLOW + "Price: " + ChatColor.AQUA + price);
            lore.add(ChatColor.GRAY + "---------------");
            newMeta.setLore(lore);
            item.setItemMeta(newMeta);
            shopInventory.addItem(item);
            p.getInventory().removeItem(item);
            p.sendMessage(StringStorage.at + ChatColor.GREEN + "You have posted your " + ChatColor.RESET + item.getItemMeta().getDisplayName() + ChatColor.GREEN + " for " + ChatColor.AQUA + price + ChatColor.GREEN + "!");
        }
    I will try removing the ChatColors, thank you!

    I have posted it in the post above.

    Without the ChatColors, it won't work at all. It throws me this error:
    Code:
    [09:06:53 ERROR]: Could not pass event InventoryClickEvent to Trades v1.0.0.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot-BT45_1.8.8_10.21.jar:git-Spigot-5f38d38-18fbb24]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-BT45_1.8.8_10.21.jar:git-Spigot-5f38d38-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-BT45_1.8.8_10.21.jar:git-Spigot-5f38d38-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-BT45_1.8.8_10.21.jar:git-Spigot-5f38d38-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:1630) [spigot-BT45_1.8.8_10.21.jar:git-Spigot-5f38d38-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInWindowClick.a(SourceFile:31) [spigot-BT45_1.8.8_10.21.jar:git-Spigot-5f38d38-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInWindowClick.a(SourceFile:9) [spigot-BT45_1.8.8_10.21.jar:git-Spigot-5f38d38-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-BT45_1.8.8_10.21.jar:git-Spigot-5f38d38-18fbb24]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_11]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_11]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot-BT45_1.8.8_10.21.jar:git-Spigot-5f38d38-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot-BT45_1.8.8_10.21.jar:git-Spigot-5f38d38-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-BT45_1.8.8_10.21.jar:git-Spigot-5f38d38-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-BT45_1.8.8_10.21.jar:git-Spigot-5f38d38-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-BT45_1.8.8_10.21.jar:git-Spigot-5f38d38-18fbb24]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_11]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 2
            at java.util.Arrays$ArrayList.get(Unknown Source) ~[?:1.8.0_11]
            at com.infinity.trades.classes.ListingsInventory.buyItem(ListingsInventory.java:83) ~[?:?]
            at com.infinity.trades.classes.ListingsInventory.inventoryClickEvent(ListingsInventory.java:41) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_11]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_11]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_11]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_11]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-BT45_1.8.8_10.21.jar:git-Spigot-5f38d38-18fbb24]
            ... 15 more
    Full Class Code:
    Code:
    package com.infinity.trades.classes;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.inventory.InventoryCloseEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class ListingsInventory implements Listener{
    
        static List<Player> confirm_list = new ArrayList<Player>();
        static List<Player> return_list = new ArrayList<Player>();
       
        public static Inventory shopInventory = Bukkit.createInventory(null, 54, "Trade Listings");
        static{
           
        }
       
        @EventHandler
        public void inventoryClickEvent(InventoryClickEvent e){
           
            Player p = (Player) e.getWhoClicked();
            ItemStack clicked = e.getCurrentItem();
            Inventory inventory = e.getInventory();
           
            if(inventory.getName().equals(shopInventory.getName())){
                if(clicked.getType() != null || clicked.getType() != Material.AIR){
                   
                    e.setCancelled(true);
                   
                    buyItem(clicked, p);
                   
                }
            }
           
        }
       
        @EventHandler
        public void inventoryCloseEvent(InventoryCloseEvent e){
            if(e.getInventory().getName().equals(shopInventory.getName())){
                if(confirm_list.contains(e.getPlayer())){
                    confirm_list.remove(e.getPlayer());
                }else if(return_list.contains(e.getPlayer())){
                    return_list.remove(e.getPlayer());
                }else{
                    return;
                }
            }
        }
       
        public static void postItem(ItemStack item, Player p, int price){
            ItemMeta newMeta = item.getItemMeta();
            List<String> lore = new ArrayList<String>();
            lore.add(ChatColor.GRAY + "---------------");
            lore.add(ChatColor.AQUA + "Owner: " + ChatColor.RESET + p.getName());
            lore.add(ChatColor.AQUA + "Price: " + ChatColor.RESET + price);
            lore.add(ChatColor.GRAY + "---------------");
            newMeta.setLore(lore);
            item.setItemMeta(newMeta);
            shopInventory.addItem(item);
            p.getInventory().removeItem(item);
            p.sendMessage(StringStorage.at + ChatColor.GREEN + "You have posted your " + ChatColor.RESET + item.getItemMeta().getDisplayName() + ChatColor.GREEN + " for " + ChatColor.AQUA + price + ChatColor.GREEN + "!");
        }
       
        public static void buyItem(ItemStack item, Player p){
            List<String> lore = item.getItemMeta().getLore();
            String loreowner = lore.get(1);
            String loreprice = lore.get(2);
            String[] owner_words = loreowner.split("\\W+");
            String[] price_words = loreprice.split("\\W+");
            List<String> ownerlist = Arrays.asList(owner_words);
            List<String> pricelist = Arrays.asList(price_words);
            String owner = ownerlist.get(2);
            String price = pricelist.get(2);
            if(owner.equals(p.getName())){
                if(return_list.contains(p)){
                    return_list.remove(p);
                    shopInventory.removeItem(item);
                    List<String> newLore = new ArrayList<String>();
                    item.getItemMeta().setLore(newLore);
                    p.getInventory().addItem(item);
                    p.sendMessage(StringStorage.at + ChatColor.RED + "You took back this item.");
                }else if(!return_list.contains(p)){
                    return_list.add(p);
                    p.sendMessage(StringStorage.at + ChatColor.GREEN + "If you really want to take back this item, click it again. If not, exit the Trades Inventory.");
                }
            }else{
                if(confirm_list.contains(p)){
                    confirm_list.remove(p);
                    Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "eco give " + owner + " " + price);
                    Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "eco take " + p.getName() + " " + price);
                    shopInventory.removeItem(item);
                    List<String> newLore = new ArrayList<String>();
                    item.getItemMeta().setLore(newLore);
                    p.getInventory().addItem(item);
                    p.sendMessage(StringStorage.at + ChatColor.GREEN + "You bought " + ChatColor.RESET + item.getItemMeta().getDisplayName() + ChatColor.GREEN + " for " + ChatColor.AQUA + "$" + price + ChatColor.GREEN + " from " + ChatColor.AQUA + owner + ChatColor.GREEN + "!");
                }else if(!confirm_list.contains(p)){
                    p.sendMessage(StringStorage.at + ChatColor.GREEN + "If you really want to buy this item, click it again. If not, exit the Trades Inventory.");
                    confirm_list.add(p);
                }
            }
        }
       
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jan 13, 2016
  6. @MCJoshua345 You should create a custom object to store them easier, but:
    String loreowner;
    String owner = loreowner.split(": ").substring(2);

    Same applies for price
     
  7. Offline

    MCJoshua345

    Cannot invoke substring(int) on the array type String[]
    I get this error.

    @Juancomaster1998 @87pen @Zombie_Striker

    Thanks for all your help. I got it working by making the whole lore line 1 ChatColor.

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

    Zombie_Striker

    @MCJoshua345
    Do you own Infinity.com? If you don't, then use "me.infinity..."
    This should be your bigger problem, since it should not be that colors fix your code.

    Are you sure you want to skip over the first line? Since arrays start at "0".

    Are you sure you will always be able to get the third object? Are you sure it will always split into three strings?
     
  9. Offline

    MCJoshua345

    I don't. I'll change that in future plugins.

    I'm boggled at that too. But for now, the plugin works perfectly.

    I know. But the first line of lore is "---------------", so that's why it's one.

    Unless Mojang makes it to where usernames can contain spaces, it will be good.
     
Thread Status:
Not open for further replies.

Share This Page