YamlConfiguration error Bukkit

| | August 4, 2015

I’m making a plugin for craftbukkit 1.8.7-R_01 (from BuildTools) version.

Error message:

[14:47:30] [Server thread/ERROR]: Could not pass event InventoryCloseEvent to ServerAssets v1.0
org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot.jar:git-Spigot-f928e7a-994b2aa]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-f928e7a-994b2aa]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-f928e7a-994b2aa]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot.jar:git-Spigot-f928e7a-994b2aa]
    at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.handleInventoryCloseEvent(CraftEventFactory.java:838) [spigot.jar:git-Spigot-f928e7a-994b2aa]
    at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:1384) [spigot.jar:git-Spigot-f928e7a-994b2aa]
    at net.minecraft.server.v1_8_R3.PacketPlayInCloseWindow.a(PacketPlayInCloseWindow.java:18) [spigot.jar:git-Spigot-f928e7a-994b2aa]
    at net.minecraft.server.v1_8_R3.PacketPlayInCloseWindow.a(PacketPlayInCloseWindow.java:1) [spigot.jar:git-Spigot-f928e7a-994b2aa]
    at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-f928e7a-994b2aa]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_45]
    at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_45]
    at net.minecraft.server.v1_8_R3.SystemUtils.a(SystemUtils.java:19) [spigot.jar:git-Spigot-f928e7a-994b2aa]
    at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:718) [spigot.jar:git-Spigot-f928e7a-994b2aa]
    at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:367) [spigot.jar:git-Spigot-f928e7a-994b2aa]
    at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:657) [spigot.jar:git-Spigot-f928e7a-994b2aa]
    at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:560) [spigot.jar:git-Spigot-f928e7a-994b2aa]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_45]
Caused by: java.lang.NullPointerException
    at com.isoStudios.main.BackPack.save(BackPack.java:105) ~[?:?]
    at com.isoStudios.main.BackPack.inventoryClose(BackPack.java:99) ~[?:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_45]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_45]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_45]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_45]

The error message say, that there is an error in line 105:

config.set(""+i, item.serialize());

The class source code:

package com.isoStudios.main;

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

import org.bukkit.Bukkit;
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.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.InventoryCloseEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

public class BackPack implements CommandExecutor, Listener {

    YamlConfiguration config;

    public BackPack(YamlConfiguration config) {
        this.config = config;
    }

    Inventory inventory = Bukkit.createInventory(null, 9, "BackPack");
    Player player;
    boolean opened = false;

    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {

        boolean cancelled = true;

        if (sender instanceof Player) {
            player = (Player) sender;

            ItemStack[] inv = player.getInventory().getContents();
            for (ItemStack item : inv) {
                if (item != null) {
                    if (item.getEnchantmentLevel(Enchantment.DIG_SPEED) == 10 && item.getEnchantmentLevel(Enchantment.DURABILITY) == 10 && item.getType().equals(Material.BOOK)) {
                        File file = new File("plugins/ServerAssets/saves/" + player.getName() + ".yml");
                        inventory = Bukkit.createInventory(player, 9, "BackPack");
                        if (file.exists()) {
                            try {
                                config.load(file);
                            } catch (Exception ex) {
                                Bukkit.getLogger().info("Failed to load config file, while trying to restore backpack content!");
                            }
                            for (int j = 0; j < 9; j++) {
                                ItemStack i = config.getItemStack(""+j);
                                inventory.setItem(j, i);
                            }
                        } else {
                            try {
                                file.createNewFile();
                            } catch (IOException e) {
                                Bukkit.getLogger().info("Failed to create player save file!");
                                e.printStackTrace();
                            }
                        }
                        player.openInventory(inventory);
                        cancelled = false;
                    }
                }
            }
            if (cancelled) {
                player.sendMessage(ChatColor.BLUE + "You don't have a backpack!");
                return true;
            }
        } else {
            sender.sendMessage(ChatColor.RED + "You need to be a player, to use this command!");
        }

        return false;
    }

    @EventHandler
    public void inventoryClose(InventoryCloseEvent event) { 
        File file = new File("plugins/ServerAssets/saves/" + event.getPlayer().getName() + ".yml");
        if (!(new File("plugins/ServerAssets/saves*" + event.getPlayer().getName() + ".yml").exists())) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                Bukkit.getLogger().info("Failed to create user save file!");
                e.printStackTrace();
            }
        }
        List<ItemStack> items = new ArrayList<ItemStack>();

        for (ItemStack i : inventory.getContents()) {
            items.add(i);
        }

        save(items, file);
    }

    public void save(List<ItemStack> items, File file) {
        int i = 0;
        for (ItemStack item : items) {
            config.set(""+i, item.serialize());
        }
        try {
            config.save(file);
        } catch (IOException e) {
            Bukkit.getLogger().info("Failed to save config file while saving backpack content!");
            e.printStackTrace();
        }
    }
}

One Response to “YamlConfiguration error Bukkit”

  1. ItemStack, according to the Bukkit API, is by default ConfigurationSerializable, so you can simply use

     config.set(""+i, item); 
    

    and then to retrieve it from the config you would use

     is = (ItemStack) getConfig().get("example");
    

    The reason why you’re getting a NullPointerException is because the serialize function converts it into a Map.

Leave a Reply