Get itemstack from hand and save name to config?

Discussion in 'Plugin Development' started by AdamDev, Sep 28, 2017.

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

    AdamDev

    Hello, AdamDev here.

    I've been having a problem saving the name of the item stack to config. Everytime that it saves, it saves it as:

    Code:
    item: !!org.bukkit.Material 'COBBLESTONE'
    
    How can I make it just to say:
    Code:
    item: COBBLESTONE
    
    If you could help it would be great :D

    Config.yml:
    Code:
    Blackmarket:
      playersSelling: {}
      inventory:
        l0__ol:
          selling:
            item: !!org.bukkit.Material 'COBBLESTONE'
            amount: 1
            price: 1
            priceall: 1
    
    Sell.java Class:
    Code:
    package com.gc.blackmarket.commands;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    
    import com.gc.blackmarket.Core;
    import com.gc.blackmarket.inventorys.Blackmarket;
    
    public class Sell implements CommandExecutor {
        // /bmsi <amount> <price>
        String igprefix = "§7[§0BlackMarket§7] §8> §r";
        Help help = new Help();
        Core plugin = Core.getInstance();
        Blackmarket bm = new Blackmarket();
        public static int count = 0;
       
        public boolean onCommand(CommandSender sender, Command cmd, String l, String[] a) {
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED+"You must be a 'Player' to run this command!");
                return true;
            }
            Player p = (Player) sender;
            int length = a.length;
            if (cmd.getName().equalsIgnoreCase("bmsi")){
                if (length == 0) {
                    help.missingArgs(p, "/bmsi <amount> <price> - Remember to have a item in your hand!");
                    return true;
                }
                if (length == 1) {
                    help.missingArgs(p, "/bmsi <amount> <price> - Remember to have a item in your hand!");
                    return true;
                }
                if (length == 2) {
                    if (!isInt(a[0]) && !isInt(a[1])) {
                        p.sendMessage(igprefix+ChatColor.RED+"Invalid Arguments. Must be a number.");
                        p.sendMessage(igprefix+ChatColor.GOLD+"Usage: /bmsi <amount> <price>");
                    } else if (!isInt(a[0]) && isInt(a[1])) {
                        p.sendMessage(igprefix+ChatColor.RED+"Invalid Argument: "+a[0]+". Must be a number.");
                        p.sendMessage(igprefix+ChatColor.GOLD+"Usage: /bmsi <amount> <price>");
                    } else if (isInt(a[0]) && !isInt(a[1])) {
                        p.sendMessage(igprefix+ChatColor.RED+"Invalid Argument: "+a[1]+". Must be a number.");
                        p.sendMessage(igprefix+ChatColor.GOLD+"Usage: /bmsi <amount> <price>");
                    } else {
                    ItemStack item = p.getInventory().getItemInMainHand();
                    if (item == null || item.getType() == Material.AIR) {
                        p.sendMessage(igprefix+ChatColor.RED+"You don't have a item in your hand!");
                        return true;
                    } else {
                        PlayerInventory inv = p.getInventory();
                        ItemStack is = inv.getItemInMainHand();
                        int amt = is.getAmount();
                        int selling = Integer.parseInt(a[0]);
                        int price = Integer.parseInt(a[1]);
                        int all = price * selling;
                        if (amt == selling) {
                            if (plugin.getConfig().getString("Blackmarket.inventory."+p.getName()) != null) {
                                p.sendMessage(igprefix+ChatColor.RED+"You currently could only sell one item!");
                                return true;
                            } else if (plugin.getConfig().getString("Blackmarket.inventory."+p.getName()) == null) {
                                plugin.getConfig().set("Blackmarket.inventory."+p.getName()+".selling.item", is.getType());
                                plugin.getConfig().set("Blackmarket.inventory."+p.getName()+".selling.amount", selling);
                                plugin.getConfig().set("Blackmarket.inventory."+p.getName()+".selling.price", price);
                                plugin.getConfig().set("Blackmarket.inventory."+p.getName()+".selling.priceall", all);
                                plugin.saveConfig();
                                p.sendMessage(igprefix+ChatColor.GREEN+"Item Sold!");
                                Core.players.add(p.getName());
                                is = null;
                                return true;
                            }
                        } else if (amt > selling) {
                            if (plugin.getConfig().getString("Blackmarket.inventory."+p.getName()) != null) {
                                p.sendMessage(igprefix+ChatColor.RED+"You currently could only sell one item!");
                                return true;
                            } else if (plugin.getConfig().getString("Blackmarket.inventory."+p.getName()) == null) {
                                plugin.getConfig().set("Blackmarket.inventory."+p.getName()+".selling.item", is.getType());
                                plugin.getConfig().set("Blackmarket.inventory."+p.getName()+".selling.amount", selling);
                                plugin.getConfig().set("Blackmarket.inventory."+p.getName()+".selling.price", price);
                                plugin.getConfig().set("Blackmarket.inventory."+p.getName()+".selling.priceall", all);
                                plugin.saveConfig();
                                p.sendMessage(igprefix+ChatColor.GREEN+"Item Sold!");
                                Core.players.add(p.getName());
                                is.setAmount(amt - selling);
                                return true;
                            }
                        } else if (amt < selling) {
                            p.sendMessage(igprefix+ChatColor.RED+"You don't have enough of this item!");
                            return true;
                        }               
                    }
                }
                if (length >= 3) {
                    help.tooManyArgs(p, "/bmsi <amount> <price> - Remember to have a item in your hand!");
                    return true;
                }
            }
        }
            return true;
        }
        public boolean isInt(String string) {
            try {
                Integer.parseInt(string);
                return true;
            } catch (NumberFormatException e) {
                return false;
            }
        }
    }
    
    Thanks for your help!

    ~ AdamDev
     
  2. Offline

    krizzdawg

    Use item.getType().name()
     
  3. Offline

    VictoryShot

    This will print out: COBBLESTONE
    Code:
    ItemStack is = new ItemStack(Material.COBBLESTONE);
            System.out.println(is.getType().name().toUpperCase());
     
  4. Offline

    Zombie_Striker

    @VictoryShot
    You don't need to call toUpperCase. The name should always be uppercase.
     
  5. Offline

    AdamDev

    @Zombie_Striker is right. It will always print COBBLESTONE or EGG, etc.

    One more problem, Now how can I take this name and turn it into an item to set it into an inventory?

    This is my Open.java class:
    There is others but not useful to this.
    Code:
    // ^^ already in config
    for (int i = 0;i<=bm.blackMarket().getContents().length;i++) {
                        for (int x = 0;x<Core.players.size();x++) {
                            Material mat = Material.getMaterial(plugin.getConfig().getString("Blackmarket.inventory."+Core.players.get(x)+".selling.item"));
                            ItemStack is = new ItemStack(mat);
                            ItemMeta ism = is.getItemMeta();
                            ArrayList<String> isl = new ArrayList<String>();
                            int price = plugin.getConfig().getInt("Blackmarket.inventory."+Core.players.get(x)+".selling.price");
                            int amt = plugin.getConfig().getInt("Blackmarket.inventory."+Core.players.get(x)+".selling.price");
                            int all = price * amt;
                            ism.setDisplayName("§b"+plugin.getConfig().getString("Blackmarket.inventory."+Core.players.get(x)+".selling.item"));
                            isl.add("§bPrice: §e"+price);
                            isl.add("§bAmount: §e"+amt);
                            isl.add("§bAll: §e"+all);
                            ism.setLore(isl);
                            is.setItemMeta(ism);
                            if (i == 45) {
                                break;
                            } else if (i <= 44) {
                                bm.blackMarket().setItem(i, is);
                            }
                        }
                    }
                    p.sendMessage(igprefix+ChatColor.YELLOW+"Opening...");
                    p.openInventory(bm.blackMarket());
                    return true;
                }
    // VV to many arguments
    
    I don't know if I'm doing this right.
    Also the inventory:
    Code:
    public Inventory blackMarket() {
            Inventory inv = Bukkit.createInventory(null, 54, "§0§lBlack Market");
            //
            ItemStack iys = new ItemStack(Material.BOOK,1);
            ItemStack ie = new ItemStack(Material.CHEST,1);
            ItemStack np = new ItemStack(Material.EXP_BOTTLE,1);
            ItemStack pp = new ItemStack(Material.GLASS_BOTTLE,1);
            //
            ItemMeta iysm = iys.getItemMeta();
            ItemMeta iem = ie.getItemMeta();
            ItemMeta npm = np.getItemMeta();
            ItemMeta ppm = pp.getItemMeta();
            //
            ArrayList<String> iysl = new ArrayList<String>();
            ArrayList<String> iel = new ArrayList<String>();
            ArrayList<String> npl = new ArrayList<String>();
            ArrayList<String> ppl = new ArrayList<String>();
            //
            iysm.setDisplayName("§bItems your selling");
            iysl.add("§7Click me! This will show all the items your selling.");
            iem.setDisplayName("§cItems Expired");
            iel.add("§7Click me! This will show the items that have expired.");
            npm.setDisplayName("§aNext page");
            npl.add("§7Next page in the Black Market.");
            ppm.setDisplayName("§cPrevious page");
            ppl.add("§7Previous page in the Black Market.");
            //
            iysm.setLore(iysl);
            iem.setLore(iel);
            npm.setLore(npl);
            ppm.setLore(ppl);
            //
            iys.setItemMeta(iysm);
            ie.setItemMeta(iem);
            np.setItemMeta(npm);
            pp.setItemMeta(ppm);
            //
            inv.setItem(48, iys);
            inv.setItem(50, ie);
            inv.setItem(53, np);
            inv.setItem(45, pp);
            //
            return inv;
        }
    
    Nothing so special about that.

    Thanks!
     
  6. Offline

    Minesuchtiiii

    Just use something like

    Code:
    ItemStack item = new ItemStack(Material.getMaterial(YourConfigString));
    yourinventory.setItem(slot, item);
     
  7. Offline

    AdamDev

    @Minesuchtiiii
    I don't think you saw:
    It's the same thing it doesn't work.
     
  8. Offline

    Minesuchtiiii

    This is supposed to work, maybe the string is null?
     
  9. Offline

    AdamDev

    @Minesuchtiiii
    Hmm, I think I see the problem.

    The ItemStack doesn't have a amount of items.
    So basically the ItemStack has an amount of zero. So the ItemStack is null. I'll try it out later.
     
  10. Offline

    Caderape2

    @AdamDev Nope, if you don't specify an amount, the amount will be 1 when you create the itemstack
     
  11. Offline

    AdamDev

    @Caderape2
    Yes, I know that but there's one little thing is that what if the player wants to sell 34 cobblestone? It has to have an amount any way.
     
  12. Offline

    Caderape2

  13. Offline

    AdamDev

    @Caderape2
    Its not adding the item to the inventory when opening it. So that's why I said I'm going to add an amount and going to switch something in the code.
     
  14. Offline

    Caderape2

    @AdamDev the method 'Inventory blackMarket()' look good.
    When you do player.openInventory(blackMarket()) the inventory is empty ? And the name of the inventory is the good one ?

    Try to open it outside of the method with the loop
     
  15. Offline

    AdamDev

    @Caderape2
    I think you didn't see. It is outside the loop. The inventory opens it just not getting the item from the config.

    I'm going to comment out the ItemMeta just to see if it's that or not.

    Edit: It was not. I decided to change it up to see something was up.
     
    Last edited: Oct 5, 2017
Thread Status:
Not open for further replies.

Share This Page