Solved "java.lang.IllegalArgumentException: Specified class does not exist" But it does

Discussion in 'Plugin Development' started by Conarnar, Nov 7, 2013.

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

    Conarnar

    I'm trying to make a kit plugin that uses ConfigurationSerializable to save inventories.
    When I try to load a configuration file, this happens.
    Code:
    2013-11-07 19:22:29 [SEVERE] Cannot load plugins\MorangeKits\kits.yml
    org.bukkit.configuration.InvalidConfigurationException: org.yaml.snakeyaml.error.YAMLException: Could not deserialize object
        at org.bukkit.configuration.file.YamlConfiguration.loadFromString(YamlConfiguration.java:55)
        at org.bukkit.configuration.file.FileConfiguration.load(FileConfiguration.java:138)
        at org.bukkit.configuration.file.FileConfiguration.load(FileConfiguration.java:105)
        at org.bukkit.configuration.file.YamlConfiguration.loadConfiguration(YamlConfiguration.java:175)
        at me.conarnar.morangekits.MorangeKits.onEnable(MorangeKits.java:29)
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:457)
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:381)
        at org.bukkit.craftbukkit.v1_6_R3.CraftServer.loadPlugin(CraftServer.java:284)
        at org.bukkit.craftbukkit.v1_6_R3.CraftServer.enablePlugins(CraftServer.java:266)
        at net.minecraft.server.v1_6_R3.MinecraftServer.l(MinecraftServer.java:315)
        at net.minecraft.server.v1_6_R3.MinecraftServer.f(MinecraftServer.java:292)
        at net.minecraft.server.v1_6_R3.MinecraftServer.a(MinecraftServer.java:252)
        at net.minecraft.server.v1_6_R3.DedicatedServer.init(DedicatedServer.java:152)
        at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:393)
        at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
    Caused by: org.yaml.snakeyaml.error.YAMLException: Could not deserialize object
        at org.bukkit.configuration.file.YamlConstructor$ConstructCustomObject.construct(YamlConstructor.java:37)
        at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:183)
        at org.yaml.snakeyaml.constructor.BaseConstructor.constructMapping2ndStep(BaseConstructor.java:326)
        at org.yaml.snakeyaml.constructor.SafeConstructor.constructMapping2ndStep(SafeConstructor.java:143)
        at org.yaml.snakeyaml.constructor.BaseConstructor.constructMapping(BaseConstructor.java:307)
        at org.yaml.snakeyaml.constructor.SafeConstructor$ConstructYamlMap.construct(SafeConstructor.java:459)
        at org.bukkit.configuration.file.YamlConstructor$ConstructCustomObject.construct(YamlConstructor.java:26)
        at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:183)
        at org.yaml.snakeyaml.constructor.BaseConstructor.constructDocument(BaseConstructor.java:142)
        at org.yaml.snakeyaml.constructor.BaseConstructor.getSingleData(BaseConstructor.java:128)
        at org.yaml.snakeyaml.Yaml.loadFromReader(Yaml.java:480)
        at org.yaml.snakeyaml.Yaml.load(Yaml.java:399)
        at org.bukkit.configuration.file.YamlConfiguration.loadFromString(YamlConfiguration.java:53)
        ... 15 more
    Caused by: java.lang.IllegalArgumentException: Specified class does not exist ('me.conarnar.morangekits.InventoryWrapper')
        at org.bukkit.configuration.serialization.ConfigurationSerialization.deserializeObject(ConfigurationSerialization.java:177)
        at org.bukkit.configuration.file.YamlConstructor$ConstructCustomObject.construct(YamlConstructor.java:35)
        ... 27 more
    It says
    Code:
    Caused by: java.lang.IllegalArgumentException: Specified class does not exist ('me.conarnar.morangekits.InventoryWrapper')
    but that class does exist.
    Code:
    package me.conarnar.morangekits;
     
    import java.util.HashMap;
    import java.util.Map;
     
    import org.bukkit.configuration.serialization.ConfigurationSerializable;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
     
    public class InventoryWrapper implements ConfigurationSerializable {
        private final ItemStack[] contents;
        private final ItemStack[] armorContents;
       
        public InventoryWrapper(PlayerInventory inv) {
            this(inv.getContents(), inv.getArmorContents());
        }
       
        protected InventoryWrapper(ItemStack[] stack1, ItemStack[] stack2) {
            contents = stack1;
            armorContents = stack2;
        }
       
        public Map<String, Object> serialize() {
            Map<String, Object> map = new HashMap<String, Object>();
           
            for (int slot = 0; slot < contents.length; slot++) {
                if (contents[slot] != null) {
                    map.put(String.valueOf(slot), contents[slot]);
                }
            }
           
            for (int slot = 0; slot < armorContents.length; slot++) {
                if (armorContents[slot] != null) {
                    map.put("armor" + slot, armorContents[slot]);
                }
            }
           
            return map;
        }
       
        public static InventoryWrapper deserialize(Map<String, Object> map) {
            ItemStack[] stack1 = new ItemStack[36];
            ItemStack[] stack2 = new ItemStack[4];
           
            for (int slot = 0; slot < stack1.length; slot++) {
                stack1[slot] = (ItemStack) map.get(String.valueOf(slot));
            }
           
            for (int slot = 0; slot < stack2.length; slot++) {
                stack2[slot] = (ItemStack) map.get("armor" + slot);
            }
           
            return new InventoryWrapper(stack1, stack2);
        }
       
        public void setPlayerInventory(Player player) {
            player.getInventory().setContents(contents);
            player.getInventory().setArmorContents(armorContents);
        }
    }
    
     
  2. Offline

    Chinwe

    I had this error before, have you registered the class ? Put this in your onEnable() (I read you should put it in the actual class in static {}, though that didn't work for me :()
    ConfigurationSerialization.registerClass(InventoryWrapper.class);
     
  3. Offline

    Conarnar

    Oh yeah, I forgot all about that. I'll try that later to see if it works.
     
  4. Offline

    Wolvereness Bukkit Team Member

    Use onLoad() for registering serializable classes... static{} is initiated before your plugin even exists, and action there would result in strange classloader leaks.
     
    Hellgast likes this.
  5. Offline

    1Rogue Retired Staff


    Question on ordering of that, do static blocks have same initialization time as fields?
     
  6. Offline

    Conarnar

    Still same thing happening.
    Code:java
    1. public void onEnable() {
    2. getServer().getPluginManager().registerEvents(this, this);
    3.  
    4. Command command = getCommand("mkit");
    5. command.setUsage("/mkit [create|delete] <kitname>");
    6.  
    7. kitsFile = new File(getDataFolder(), "kits.yml");
    8. kits = YamlConfiguration.loadConfiguration(kitsFile);
    9. ConfigurationSerialization.registerClass(InventoryWrapper.class);
    10. }


    Oh wait I think I see the problem.
    Code:java
    1. kits = YamlConfiguration.loadConfiguration(kitsFile);
    2. ConfigurationSerialization.registerClass(InventoryWrapper.class);

    I should have put ConfigurationSerialization.registerClass(InventoryWrapper.class) above that.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  7. Offline

    zoominx55

    Glad I found this. It solved my problem too after several hours of head scratching. What's really weird is that I had not registered any of my serializable classes (one superclass and 4 subclasses). Serialization went flawlessly for all classes. However when deserializing I got the error when attempting to access one of the sub classes. Only one. If I didn't have that class in my config it worked fine. I did have to register all of my classes to get it to get that one class to work properly.
     
    Hellgast likes this.
Thread Status:
Not open for further replies.

Share This Page