Kit plugin help!

Discussion in 'Spigot Plugin Development' started by _AlecPlays_, Jun 20, 2016.

  1. Hey guys,

    I am making a plugin that gives players kits. I want it to open up a storage window with the items for the kit. I plan on saving this by doing a command and that command will save everything in my inventory. However, I am having severe problems with this. I can save my kits fine but when I go to load then It just thinks it is empty, even when the file with the data is full.

    Here is my code:
    Code (Text):
    package me.popcorngames;

    import java.io.File;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.InventoryHolder;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;

    public class init extends JavaPlugin {
       
        @Override
        public void onEnable() {
           
            getLogger().info("Plugin Enabled!");
        }
       
        public void onDisable() {
            getLogger().info("Plugin Disabled!");
        }
       
       
        @SuppressWarnings("static-access")
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
           
            if (cmd.getName().equalsIgnoreCase("kitadd") && sender instanceof Player) {
               
                Player player = (Player) sender;
               
               
               
                int length = args.length;
               

               
                if(length == 1){
                   

                           
                     if (this.saveInventoryToFile(player.getInventory(), this.getDataFolder(), args[0])) player.sendMessage(ChatColor.RED + "Saved your inventory to: " + new File(this.getDataFolder(), args[0] + ".kitsave").getAbsolutePath());
                        else player.sendMessage(ChatColor.RED + "[JP] Could not save your inventory.");
                   
                }
                else{
                    player.sendMessage(ChatColor.RED + "[JP] Error!");
                }  
            }
           
           
           
           
           
            else if (cmd.getName().equalsIgnoreCase("kit") && sender instanceof Player) {
               
                Player player = (Player) sender;
               
                int length = args.length;
               
                if(length == 1){
               
                    Inventory playerInventory = this.getInventoryFromFile(new File(this.getDataFolder(), args[0] + ".kitsave"));
                        if (playerInventory != null) player.openInventory(playerInventory);
                        else player.sendMessage(ChatColor.RED + "No kit!");
                       
               
               
                }
            }
           
                return false;
        }
       
            public static boolean saveInventoryToFile(Inventory inventory, File path, String fileName) {
            if (inventory == null || path == null || fileName == null) return false;
            try {
                File invFile = new File(path, fileName + ".kitsave");
                if (invFile.exists()) invFile.delete();
                FileConfiguration invConfig = YamlConfiguration.loadConfiguration(invFile);

                invConfig.set("Title", inventory.getTitle());
                invConfig.set("Size", inventory.getSize());
                invConfig.set("Max stack size", inventory.getMaxStackSize());
                if (inventory.getHolder() instanceof Player) invConfig.set("Holder", ((Player) inventory.getHolder()).getName());

                ItemStack[] invContents = inventory.getContents();
                for (int i = 0; i < invContents.length; i++) {
                    ItemStack itemInInv = invContents[i];
                    if (itemInInv != null) if (itemInInv.getType() != Material.AIR) invConfig.set("Slot " + i, itemInInv);
                }

                invConfig.save(invFile);
                return true;
            } catch (Exception ex) {
                return false;
            }
        }
       
       
        public static Inventory getInventoryFromFile(File file) {
            if (file == null) return null;
            if (!file.exists() || file.isDirectory() || !file.getAbsolutePath().endsWith(".kitsave")) return null;
            try {
                FileConfiguration invConfig = YamlConfiguration.loadConfiguration(file);
                Inventory inventory = null;
                String invTitle = invConfig.getString("Title", "Inventory");
                int invSize = invConfig.getInt("Size", 27);
                int invMaxStackSize = invConfig.getInt("Max stack size", 64);
                InventoryHolder invHolder = null;
                if (invConfig.contains("Holder")) invHolder = Bukkit.getPlayer(invConfig.getString("Holder"));
                inventory = Bukkit.getServer().createInventory(invHolder, invSize, ChatColor.translateAlternateColorCodes('&', invTitle));
                inventory.setMaxStackSize(invMaxStackSize);
                try {
                    ItemStack[] invContents = new ItemStack[invSize];
                    for (int i = 0; i < invSize; i++) {
                        if (invConfig.contains("Slot " + i)) invContents[i] = invConfig.getItemStack("Slot " + i);
                        else invContents[i] = new ItemStack(Material.AIR);
                    }
                    inventory.setContents(invContents);
                } catch (Exception ex) {
                }
                return inventory;
            } catch (Exception ex) {
                return null;
            }
        }
       
       
            }
    Please, if someone could tell me what I am doing wrong that would be great! I am also really new to Minecraft plugins.

    I also got some of my code from this bukkit forum post

    Thanks!
     
  2. I think the problem is you aren't locating the right file or relating it to the right path because it is seeing it as a new file and makes a blank one with nothing inside. Maybe this?
     
  3. @_AlecPlays_ we are not a computer to identify what is exactly the error with so you should give the stacktrace aswell.
     
  4. There is no error. The plugin loads fine, it just won't see the .kitsave file for some reason
     
    #4 _AlecPlays_, Jun 20, 2016
    Last edited: Jun 20, 2016
  5. I think it is finding the file, I just don't think it can read the file for some reason
     
  6. The path to the file is wrong
     
  7. Where is the path wrong in the code, I am looking through it and I don't see where the path is wrong....
     
  8. Lets see
     
  9. I'm thinking it has to do with the method that converts the file into a inventory
     
  10. Alright