Search in sources :

Example 6 with MyPetPlayerImpl

use of de.Keyle.MyPet.util.player.MyPetPlayerImpl in project MyPet by xXKeyleXx.

the class MongoDbRepository method documentToPlayer.

// Players ---------------------------------------------------------------------------------------------------------
private MyPetPlayer documentToPlayer(Document document) {
    try {
        MyPetPlayerImpl petPlayer;
        UUID internalUUID = UUID.fromString(document.getString("internal_uuid"));
        String playerName = document.getString("name");
        // raw "get" fixes wrong data type
        UUID mojangUUID = document.get("mojang_uuid") != null ? UUID.fromString("" + document.get("mojang_uuid")) : null;
        if (mojangUUID != null) {
            petPlayer = new MyPetPlayerImpl(internalUUID, mojangUUID);
            petPlayer.setLastKnownName(playerName);
        } else if (playerName != null) {
            petPlayer = new MyPetPlayerImpl(internalUUID, playerName);
        } else {
            MyPetApi.getLogger().warning("Player with no UUID or name found!");
            return null;
        }
        try {
            petPlayer.setExtendedInfo(TagStream.readTag(((Binary) document.get("extended_info")).getData(), true));
        } catch (ZipException exception) {
            MyPetApi.getMyPetLogger().warning("Extended info of player \"" + playerName + "\" (" + mojangUUID + ") could not be loaded!");
        }
        Document jsonObject = (Document) document.get("multi_world");
        for (Object o : jsonObject.keySet()) {
            String petUUID = jsonObject.get(o.toString()).toString();
            petPlayer.setMyPetForWorldGroup(o.toString(), UUID.fromString(petUUID));
        }
        if (document.containsKey("settings")) {
            document = (Document) document.get("settings");
        }
        petPlayer.setAutoRespawnEnabled(document.getBoolean("auto_respawn"));
        petPlayer.setAutoRespawnMin(document.getInteger("auto_respawn_min"));
        petPlayer.setCaptureHelperActive(document.getBoolean("capture_mode"));
        petPlayer.setHealthBarActive(document.getBoolean("health_bar"));
        petPlayer.setPetLivingSoundVolume(document.getDouble("pet_idle_volume").floatValue());
        return petPlayer;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : MyPetPlayerImpl(de.Keyle.MyPet.util.player.MyPetPlayerImpl) ZipException(java.util.zip.ZipException) Binary(org.bson.types.Binary) IOException(java.io.IOException) Document(org.bson.Document)

Example 7 with MyPetPlayerImpl

use of de.Keyle.MyPet.util.player.MyPetPlayerImpl in project MyPet by xXKeyleXx.

the class MySqlRepository method resultSetToMyPetPlayer.

// Players ---------------------------------------------------------------------------------------------------------
private MyPetPlayer resultSetToMyPetPlayer(ResultSet resultSet) {
    try {
        if (resultSet.next()) {
            MyPetPlayerImpl petPlayer;
            UUID internalUUID = UUID.fromString(resultSet.getString("internal_uuid"));
            String playerName = resultSet.getString("name");
            UUID mojangUUID = resultSet.getString("mojang_uuid") != null ? UUID.fromString(resultSet.getString("mojang_uuid")) : null;
            if (mojangUUID != null) {
                petPlayer = new MyPetPlayerImpl(internalUUID, mojangUUID);
                petPlayer.setLastKnownName(playerName);
            } else if (playerName != null) {
                petPlayer = new MyPetPlayerImpl(internalUUID, playerName);
            } else {
                MyPetApi.getLogger().warning("Player with no UUID or name found!");
                return null;
            }
            petPlayer.setAutoRespawnEnabled(resultSet.getBoolean("auto_respawn"));
            petPlayer.setAutoRespawnMin(resultSet.getInt("auto_respawn_min"));
            petPlayer.setCaptureHelperActive(resultSet.getBoolean("capture_mode"));
            petPlayer.setHealthBarActive(resultSet.getBoolean("health_bar"));
            petPlayer.setPetLivingSoundVolume(resultSet.getFloat("pet_idle_volume"));
            try {
                petPlayer.setExtendedInfo(TagStream.readTag(resultSet.getBlob("extended_info").getBinaryStream(), true));
            } catch (ZipException exception) {
                MyPetApi.getMyPetLogger().warning("Extended info of player \"" + playerName + "\" (" + mojangUUID + ") could not be loaded!");
            }
            ResultSetMetaData metaData = resultSet.getMetaData();
            int column = resultSet.findColumn("multi_world");
            switch(metaData.getColumnTypeName(column)) {
                case "BLOB":
                    try {
                        TagCompound worldGroups = TagStream.readTag(resultSet.getBlob(column).getBinaryStream(), true);
                        for (String worldGroupName : worldGroups.getCompoundData().keySet()) {
                            String petUUID = worldGroups.getAs(worldGroupName, TagString.class).getStringData();
                            petPlayer.setMyPetForWorldGroup(worldGroupName, UUID.fromString(petUUID));
                        }
                    } catch (ZipException exception) {
                        MyPetApi.getMyPetLogger().warning("Multiworld info of player \"" + playerName + "\" (" + mojangUUID + ") could not be loaded!");
                    }
                    break;
                case "VARCHAR":
                    try {
                        JsonObject jsonObject = gson.fromJson(resultSet.getString(column), JsonObject.class);
                        for (String uuid : jsonObject.keySet()) {
                            String petUUID = jsonObject.get(uuid).getAsString();
                            petPlayer.setMyPetForWorldGroup(uuid, UUID.fromString(petUUID));
                        }
                    } catch (JsonParseException e) {
                        e.printStackTrace();
                    }
            }
            return petPlayer;
        }
    } catch (SQLException | IOException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : MyPetPlayerImpl(de.Keyle.MyPet.util.player.MyPetPlayerImpl) TagString(de.keyle.knbt.TagString) JsonObject(com.google.gson.JsonObject) ZipException(java.util.zip.ZipException) TagString(de.keyle.knbt.TagString) IOException(java.io.IOException) JsonParseException(com.google.gson.JsonParseException) TagCompound(de.keyle.knbt.TagCompound)

Aggregations

MyPetPlayerImpl (de.Keyle.MyPet.util.player.MyPetPlayerImpl)7 MyPetPlayer (de.Keyle.MyPet.api.player.MyPetPlayer)3 IOException (java.io.IOException)3 UUID (java.util.UUID)3 JsonObject (com.google.gson.JsonObject)2 JsonParseException (com.google.gson.JsonParseException)2 MyPet (de.Keyle.MyPet.api.entity.MyPet)2 StoredMyPet (de.Keyle.MyPet.api.entity.StoredMyPet)2 SqLiteRepository (de.Keyle.MyPet.repository.types.SqLiteRepository)2 TagCompound (de.keyle.knbt.TagCompound)2 TagString (de.keyle.knbt.TagString)2 ZipException (java.util.zip.ZipException)2 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)2 WorldGroup (de.Keyle.MyPet.api.WorldGroup)1 MyPetPlayerJoinEvent (de.Keyle.MyPet.api.event.MyPetPlayerJoinEvent)1 RepositoryCallback (de.Keyle.MyPet.api.repository.RepositoryCallback)1 ExperienceCalculatorManager (de.Keyle.MyPet.api.skill.experience.ExperienceCalculatorManager)1 PluginHook (de.Keyle.MyPet.api.util.hooks.PluginHook)1 MyPetLogger (de.Keyle.MyPet.api.util.logger.MyPetLogger)1 MongoDbRepository (de.Keyle.MyPet.repository.types.MongoDbRepository)1