Search in sources :

Example 11 with SkinModel

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

the class SkinStorage method save.

public void save(UserPreference preferences) {
    SkinModel targetSkin = preferences.getTargetSkin().orElse(null);
    if (targetSkin != null && !targetSkin.isSaved()) {
        throw new IllegalArgumentException("Tried saving preferences without skin");
    }
    preferences.getSaveLock().lock();
    try (Connection con = dataSource.getConnection()) {
        if (preferences.isSaved()) {
            if (targetSkin == null) {
                try (PreparedStatement stmt = con.prepareStatement("DELETE FROM " + USER_TABLE + " WHERE UserID=?")) {
                    stmt.setInt(1, preferences.getRowId());
                    stmt.executeUpdate();
                }
            } else {
                try (PreparedStatement stmt = con.prepareStatement("UPDATE " + USER_TABLE + " SET TargetSkin=? WHERE UserID=?")) {
                    stmt.setInt(1, targetSkin.getRowId());
                    stmt.setInt(2, preferences.getRowId());
                    stmt.executeUpdate();
                }
            }
        } else {
            if (targetSkin == null) {
                // don't save empty preferences
                return;
            }
            String insertQuery = "INSERT INTO " + USER_TABLE + " (UUID, TargetSkin, KeepSkin) " + "VALUES (?, ?, ?)";
            try (PreparedStatement stmt = con.prepareStatement(insertQuery, RETURN_GENERATED_KEYS)) {
                stmt.setString(1, UUIDTypeAdapter.toMojangId(preferences.getUuid()));
                stmt.setInt(2, targetSkin.getRowId());
                stmt.setBoolean(3, preferences.isKeepSkin());
                stmt.executeUpdate();
                try (ResultSet generatedKeys = stmt.getGeneratedKeys()) {
                    if (generatedKeys != null && generatedKeys.next()) {
                        preferences.setRowId(generatedKeys.getInt(1));
                    }
                }
            }
        }
    } catch (SQLException sqlEx) {
        logger.error("Failed to save preferences for: {}", preferences, sqlEx);
    } finally {
        preferences.getSaveLock().unlock();
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) SkinModel(com.github.games647.changeskin.core.model.skin.SkinModel)

Example 12 with SkinModel

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

the class SpongeSkinAPI method setPersistentSkin.

@Override
public void setPersistentSkin(Player player, UUID targetSkinId, boolean applyNow) {
    SkinModel newSkin = plugin.getCore().getStorage().getSkin(targetSkinId);
    if (newSkin == null) {
        Optional<SkinModel> downloadSkin = plugin.getCore().getSkinApi().downloadSkin(targetSkinId);
        if (downloadSkin.isPresent()) {
            newSkin = downloadSkin.get();
        }
    }
    setPersistentSkin(player, newSkin, applyNow);
}
Also used : SkinModel(com.github.games647.changeskin.core.model.skin.SkinModel)

Example 13 with SkinModel

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

the class InfoCommand method sendSkinDetails.

private void sendSkinDetails(UUID uuid, UserPreference preference) {
    Optional<Player> optPlayer = Sponge.getServer().getPlayer(uuid);
    if (optPlayer.isPresent()) {
        Player player = optPlayer.get();
        Optional<SkinModel> optSkin = preference.getTargetSkin();
        if (optSkin.isPresent()) {
            String template = plugin.getCore().getMessage("skin-info");
            String formatted = formatter.apply(template, optSkin.get());
            Text text = TextSerializers.LEGACY_FORMATTING_CODE.deserialize(formatted);
            player.sendMessage(text);
        } else {
            plugin.sendMessage(player, "skin-not-found");
        }
    }
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) Text(org.spongepowered.api.text.Text) SkinModel(com.github.games647.changeskin.core.model.skin.SkinModel)

Example 14 with SkinModel

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

the class LoginListener method setDefaultSkin.

private void setDefaultSkin(UserPreference preferences, GameProfile profile) {
    Optional<SkinModel> randomSkin = getRandomSkin();
    if (randomSkin.isPresent()) {
        SkinModel targetSkin = randomSkin.get();
        preferences.setTargetSkin(targetSkin);
        plugin.getApi().applyProperties(profile, targetSkin);
    }
}
Also used : SkinModel(com.github.games647.changeskin.core.model.skin.SkinModel)

Example 15 with SkinModel

use of com.github.games647.changeskin.core.model.skin.SkinModel 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)

Aggregations

SkinModel (com.github.games647.changeskin.core.model.skin.SkinModel)21 UUID (java.util.UUID)9 UserPreference (com.github.games647.changeskin.core.model.UserPreference)8 Connection (java.sql.Connection)3 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 SQLException (java.sql.SQLException)3 ProxiedPlayer (net.md_5.bungee.api.connection.ProxiedPlayer)3 SkinProperty (com.github.games647.changeskin.core.model.skin.SkinProperty)2 TextureModel (com.github.games647.changeskin.core.model.skin.TextureModel)2 TextureType (com.github.games647.changeskin.core.model.skin.TextureType)2 ChangeSkinBungee (com.github.games647.changeskin.bungee.ChangeSkinBungee)1 ChangeSkinCore (com.github.games647.changeskin.core.ChangeSkinCore)1 NotPremiumException (com.github.games647.changeskin.core.NotPremiumException)1 RateLimitException (com.github.games647.changeskin.core.RateLimitException)1 SkinStorage (com.github.games647.changeskin.core.SkinStorage)1 CheckPermMessage (com.github.games647.changeskin.core.message.CheckPermMessage)1 PermResultMessage (com.github.games647.changeskin.core.message.PermResultMessage)1 SkinUpdateMessage (com.github.games647.changeskin.core.message.SkinUpdateMessage)1 GameProfile (com.github.games647.changeskin.core.model.GameProfile)1