package net.glowstone.io.entity;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import net.glowstone.entity.objects.GlowMinecart;
import net.glowstone.io.nbt.NbtSerialization;
import net.glowstone.util.nbt.CompoundTag;
import org.bukkit.Location;
import org.bukkit.inventory.InventoryHolder;

public class MinecartStore extends EntityStore<GlowMinecart> {

    private GlowMinecart.MinecartType type;

    public MinecartStore(GlowMinecart.MinecartType type) {
        super((Class<GlowMinecart>) type.getMinecartClass(), type.getEntityType());
        this.type = type;
    }

    @Override
    public GlowMinecart createEntity(Location location, CompoundTag compound) {
        try {
            Constructor<? extends GlowMinecart> constructor = type.getMinecartClass()
                .getConstructor(Location.class);
            return constructor.newInstance(location);
        } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void load(GlowMinecart entity, CompoundTag tag) {
        super.load(entity, tag);
        if (entity instanceof InventoryHolder) {
            InventoryHolder inv = (InventoryHolder) entity;
            if (inv.getInventory() != null) {
                inv.getInventory().setContents(NbtSerialization
                    .readInventory(tag.getCompoundList("Items"), 0, inv.getInventory().getSize()));
            }
        }
        // todo
    }

    @Override
    public void save(GlowMinecart entity, CompoundTag tag) {
        super.save(entity, tag);
        if (entity instanceof InventoryHolder) {
            InventoryHolder inv = (InventoryHolder) entity;
            if (inv.getInventory() != null) {
                tag.putCompoundList("Items",
                    NbtSerialization.writeInventory(inv.getInventory().getContents(), 0));
            }
        }
        // todo
    }
}

@@ -43,80 +43,26 @@
 ava.
-lang.reflect.Constructor;%0Aimport java.lang.reflect.InvocationTargetExcep
+util.function.Func
 tion
@@ -667,38 +667,35 @@
     
-try %7B%0A            Constructor%3C
+Function%3C? super Location, 
 ? ex
@@ -715,23 +715,19 @@
 ecart%3E c
-onstruc
+rea
 tor = ty
@@ -736,311 +736,82 @@
 .get
-MinecartClass()%0A                .getConstructor(Location.class);%0A            return constructor.newInstance(location);%0A        %7D catch (InstantiationException %7C IllegalAccessException %7C NoSuchMethodException %7C InvocationTargetException e) %7B%0A            e.printStackTrace();%0A        %7D%0A        return null
+Creator();%0A        return creator == null ? null : creator.apply(location)
 ;%0A  

Fix CheckStyle issues in io/entity/MinecartStore.java (#728)