Search in sources :

Example 1 with TextureModel

use of com.github.games647.changeskin.core.model.skin.TextureModel in project ChangeSkin by games647.

the class SkinFormatter method apply.

@Override
public String apply(String template, SkinModel skin) {
    if (template == null) {
        return null;
    }
    int rowId = skin.getRowId();
    UUID ownerId = skin.getProfileId();
    String ownerName = skin.getProfileName();
    long timeFetched = skin.getTimestamp();
    Map<TextureType, TextureModel> textures = skin.getTextures();
    Optional<TextureModel> skinTexture = Optional.ofNullable(textures.get(TextureType.SKIN));
    String skinUrl = skinTexture.map(TextureModel::getUrl).orElse("");
    String metadata = skinTexture.map(TextureModel::getMetadata).map(MetadataModel::getModel).orElse("");
    String capeUrl = Optional.ofNullable(textures.get(TextureType.CAPE)).map(TextureModel::getUrl).orElse("");
    String time = LocalDateTime.ofInstant(Instant.ofEpochMilli(timeFetched), ZoneId.systemDefault()).toString();
    return template.replace("{0}", Integer.toString(rowId)).replace("{1}", ownerId.toString()).replace("{2}", ownerName).replace("{3}", time).replace("{4}", skinUrl).replace("{5}", metadata).replace("{6}", capeUrl);
}
Also used : TextureModel(com.github.games647.changeskin.core.model.skin.TextureModel) UUID(java.util.UUID) TextureType(com.github.games647.changeskin.core.model.skin.TextureType)

Example 2 with TextureModel

use of com.github.games647.changeskin.core.model.skin.TextureModel in project ChangeSkin by games647.

the class SkinStorage method save.

public boolean save(SkinModel skinData) {
    if (skinData == null) {
        return false;
    }
    skinData.getSaveLock().lock();
    try {
        if (skinData.isSaved()) {
            // skin already saved
            return true;
        }
        TextureModel skinTexture = skinData.getTextures().get(SKIN);
        String skinUrl = "";
        boolean slimModel = false;
        if (skinTexture != null) {
            skinUrl = skinTexture.getShortUrl();
            MetadataModel metadata = skinTexture.getMetadata();
            if (metadata != null) {
                slimModel = true;
            }
        }
        TextureModel capeTexture = skinData.getTextures().get(CAPE);
        String capeUrl = "";
        if (capeTexture != null) {
            capeUrl = capeTexture.getShortUrl();
        }
        try (Connection con = dataSource.getConnection();
            PreparedStatement stmt = con.prepareStatement("INSERT INTO " + DATA_TABLE + " (Timestamp, UUID, Name, SlimModel, SkinURL, CapeURL, Signature) VALUES" + " (?, ?, ?, ?, ?, ?, ?)", RETURN_GENERATED_KEYS)) {
            stmt.setLong(1, skinData.getTimestamp());
            stmt.setString(2, UUIDTypeAdapter.toMojangId(skinData.getProfileId()));
            stmt.setString(3, skinData.getProfileName());
            stmt.setBoolean(4, slimModel);
            stmt.setString(5, skinUrl);
            stmt.setString(6, capeUrl);
            stmt.setBytes(7, Base64.getDecoder().decode(skinData.getSignature()));
            stmt.executeUpdate();
            try (ResultSet generatedKeys = stmt.getGeneratedKeys()) {
                if (generatedKeys != null && generatedKeys.next()) {
                    skinData.setRowId(generatedKeys.getInt(1));
                    return true;
                }
            }
        }
    } catch (SQLException sqlEx) {
        logger.error("Failed to query skin data: {}", skinData, sqlEx);
    } finally {
        skinData.getSaveLock().unlock();
    }
    return false;
}
Also used : TextureModel(com.github.games647.changeskin.core.model.skin.TextureModel) MetadataModel(com.github.games647.changeskin.core.model.skin.MetadataModel) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Aggregations

TextureModel (com.github.games647.changeskin.core.model.skin.TextureModel)2 MetadataModel (com.github.games647.changeskin.core.model.skin.MetadataModel)1 TextureType (com.github.games647.changeskin.core.model.skin.TextureType)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 UUID (java.util.UUID)1