package valandur.webapi.json.serializer.entity;

import org.spongepowered.api.data.manipulator.mutable.entity.TameableData;
import valandur.webapi.WebAPI;
import valandur.webapi.api.cache.ICacheService;
import valandur.webapi.api.cache.entity.ICachedEntity;
import valandur.webapi.api.json.WebAPIBaseSerializer;

import java.io.IOException;
import java.util.Optional;
import java.util.UUID;

public class TameableDataSerializer extends WebAPIBaseSerializer<TameableData> {
    @Override
    public void serialize(TameableData value) throws IOException {
        writeStartObject();
        writeField("isTamed", value.owner().exists());

        UUID uuid = value.owner().getDirect().orElse(Optional.empty()).orElse(null);
        if (uuid != null) {
            ICacheService srv = WebAPI.getCacheService();
            ICachedEntity owner = srv.getPlayer(uuid).orElse(null);
            if (owner == null) owner = srv.getEntity(uuid).orElse(null);

            writeField("owner", owner);
        }
        writeEndObject();
    }
}

@@ -233,22 +233,22 @@
 che.
-entity
+player
 .ICached
 Enti
@@ -243,22 +243,22 @@
 .ICached
-Entity
+Player
 ;%0Aimport
@@ -825,87 +825,14 @@
 ched
-Entity owner = srv.getPlayer(uuid).orElse(null);%0A            if (owner == null)
+Player
  own
@@ -847,14 +847,14 @@
 .get
-Entity
+Player
 (uui
@@ -870,17 +870,16 @@
 (null);%0A
-%0A
         

Fix tameable data not working with new cached player object