Solved Need help with getinventory array to config!

Discussion in 'Plugin Development' started by danablend, Jan 22, 2016.

Thread Status:
Not open for further replies.
  1. Hey guys so im working on a /Createkit feature to the plugin im making and im kinda stuck on the args part because i want /createkit to return a syntax message to the player and /createkit <kit name> to make the kit. Problem is the args always fail when i try? Any ideas?

    My command code:

    Code:
                if (cmd.getName().equalsIgnoreCase("createkit") && sender instanceof Player) {
                    Player player = (Player)sender;
                    if (player.hasPermission("echoplexkits.create")){
                        if (args[0] == null){
                            player.sendMessage(ChatColor.YELLOW + "/createkit <kit name>");
                            return true;
                        }
                        if (args.length >= 1 || args.length == 0){
                            ItemStack[] items = player.getInventory().getContents();
                            ItemStack[] armor = player.getInventory().getArmorContents();
                            config.set("kits." + args[0] + ".items", items);
                            config.set("kits." + args[0] + ".armor", armor);
                            saveConfig();
                            player.sendMessage(ChatColor.YELLOW + "Kit: " + args[0] + " has been successfully created!");
                            return true;
                        }
                    } else if  (!player.hasPermission("echoplexkits.create")){
                        player.sendMessage(ChatColor.DARK_RED + "You don't have permission to use that command!");
                        return true;
                    } else if (args.length != 1 || args.length != 0){
                        player.sendMessage(ChatColor.YELLOW + "/createkit <kit name>");
                        return true;
                    }
                }
     
  2. Offline

    stefvanschie

    @danablend Change your if (args[0] == null), so it checks if the length is 0.
     
  3. That doesnt work, just gives me internal server errors if i only do "/Createkit" What i want is that it says "/Createkit <kit name> when i type "/createkit" and it makes the kit if its ONLY "/createkit examplename" and if its above /Createkit <kit name> i want it to be the same as if you said /createkit

    Okay i fixed so if you only type /createkit it gives you the message. Now i need help with if you type for example /createkit example extraargs, that it sends you the message so only /createkit <name> creates the kit.

    Updated code:
    Code:
                if (cmd.getName().equalsIgnoreCase("createkit") && sender instanceof Player) {
                    Player player = (Player)sender;
                    if (player.hasPermission("echoplexkits.create")){
                        if (args.length <= 0{
                            player.sendMessage(ChatColor.YELLOW + "/createkit <kit name>");
                            return true;
                        }
                        if (args.length >= 1 || args.length == 0){
                            ItemStack[] items = player.getInventory().getContents();
                            ItemStack[] armor = player.getInventory().getArmorContents();
                            config.set("kits." + args[0] + ".items", items);
                            config.set("kits." + args[0] + ".armor", armor);
                            saveConfig();
                            player.sendMessage(ChatColor.YELLOW + "Kit: " + args[0] + " has been successfully created!");
                            return true;
                        }
                    } else if  (!player.hasPermission("echoplexkits.create")){
                        player.sendMessage(ChatColor.DARK_RED + "You don't have permission to use that command!");
                        return true;
                    } else if (args.length != 1 || args.length != 0){
                        player.sendMessage(ChatColor.YELLOW + "/createkit <kit name>");
                        return true;
                    }
                }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jan 23, 2016
  4. Offline

    SnelleFrikandel

    This should work.
    Code:
                if (cmd.getName().equalsIgnoreCase("createkit") && sender instanceof Player) {
                    Player player = (Player)sender;
                    if (player.hasPermission("echoplexkits.create")){
                        if (args.length != 1) {
                            player.sendMessage(ChatColor.YELLOW + "/createkit <kit name>");
                            return true;
                        } else {
                            ItemStack[] items = player.getInventory().getContents();
                            ItemStack[] armor = player.getInventory().getArmorContents();
                            config.set("kits." + args[0] + ".items", items);
                            config.set("kits." + args[0] + ".armor", armor);
                            saveConfig();
                            player.sendMessage(ChatColor.YELLOW + "Kit: " + args[0] + " has been successfully created!");
                            return true;
                        }
                    } else if  (!player.hasPermission("echoplexkits.create")){
                        player.sendMessage(ChatColor.DARK_RED + "You don't have permission to use that command!");
                        return true;
                }
     
  5. Thanks allot bro it works perfectly now!

    New problem: The /createkit saves the inventory of the player and armor, but it does not save into config "properly" By properly i mean that it saves into config like this example:
    kits:
    <kitname>:
    items:
    - IRON_SWORD 1
    - STONE_PICKAXE 1
    - GOLDEN_APPLE 64
    - GOLDEN_APPLE 64 1
    armor:
    - IRON_HELMET 1
    Enchants: DAMAGE_PROTECTION 3
    Lore: Beserker Helmet

    This is what it does when i do /createkit at this moment:

    - ==: org.bukkit.inventory.ItemStack
    type: MUSHROOM_SOUP
    - ==: org.bukkit.inventory.ItemStack
    type: BOWL
    - ==: org.bukkit.inventory.ItemStack
    type: BOWL
    - null
    - ==: org.bukkit.inventory.ItemStack
    type: MUSHROOM_SOUP
    - ==: org.bukkit.inventory.ItemStack
    type: MUSHROOM_SOUP
    - ==: org.bukkit.inventory.ItemStack
    type: MUSHROOM_SOUP
    - ==: org.bukkit.inventory.ItemStack
    type: MUSHROOM_SOUP
    - ==: org.bukkit.inventory.ItemStack
    type: MUSHROOM_SOUP

    How do i turn that spam into proper sorting like the proper way i described before with this code:

    Code:
                if (cmd.getName().equalsIgnoreCase("createkit") && sender instanceof Player) {
                    Player player = (Player)sender;
                    if (player.hasPermission("echoplexkits.create")){
                        if (args.length != 1) {
                            player.sendMessage(ChatColor.YELLOW + "/createkit <kit name>");
                            return true;
                        } else {
                            ItemStack[] items = player.getInventory().getContents();
                            ItemStack[] armor = player.getInventory().getArmorContents();
                            config.set("kits." + args[0] + ".items", items);
                            config.set("kits." + args[0] + ".armor", armor);
                            saveConfig();
                            player.sendMessage(ChatColor.YELLOW + "Kit: " + args[0] + " has been successfully created!");
                            return true;
                        }
                    } else if  (!player.hasPermission("echoplexkits.create")){
                        player.sendMessage(ChatColor.DARK_RED + "You don't have permission to use that command!");
                        return true;
                    }
                }
     
    Last edited: Jan 23, 2016
  6. Offline

    ShowbizLocket61

    @danablend
    Exactly what command are you running?
     
  7. the boolean is "onCommand"
    the ingame command is /createkit
    it saves an array to the config but i want the array "simplified" as i gave an example of above.
     
  8. Offline

    Zombie_Striker

    @danablend
    The reason it does that is because its using bukkits itemstack storing format. What you want requires you to make your own format for storing objects.
     
  9. Offline

    ShowbizLocket61

    @danablend
    You could use the toString() method on the itemstack.
     
  10. Offline

    Zombie_Striker

    @ShowbizLocket61
    That could work, but his problem is with the formatting. What he would have to do is create his own toString method.
     
  11. Yeah i need a serializer for this method to work, i think so. Because if i do toString() it saves 1 line in the config, i want the kits to be changeable in config so it cant work like that.
     
Thread Status:
Not open for further replies.

Share This Page