Search in sources :

Example 16 with SkinModel

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

Example 17 with SkinModel

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

the class SkinStorage method getPreferences.

public UserPreference getPreferences(UUID uuid) {
    try (Connection con = dataSource.getConnection();
        PreparedStatement stmt = con.prepareStatement("SELECT SkinId, Timestamp, " + DATA_TABLE + ".UUID, Name, SlimModel, SkinUrl, CapeUrl, Signature, " + USER_TABLE + ".*" + " FROM " + USER_TABLE + " LEFT JOIN " + DATA_TABLE + " ON " + USER_TABLE + ".TargetSkin=" + DATA_TABLE + ".SkinID" + " WHERE " + USER_TABLE + ".UUID=? LIMIT 1")) {
        stmt.setString(1, UUIDTypeAdapter.toMojangId(uuid));
        try (ResultSet resultSet = stmt.executeQuery()) {
            if (resultSet.next()) {
                int prefId = resultSet.getInt(9);
                SkinModel skinData = null;
                if (resultSet.getObject(1) != null) {
                    skinData = parseSkinData(resultSet);
                }
                boolean keepSkin = resultSet.getBoolean(12);
                return new UserPreference(prefId, uuid, skinData, keepSkin);
            } else {
                return new UserPreference(uuid);
            }
        }
    } catch (SQLException sqlEx) {
        logger.error("Failed to query preferences {}", uuid, sqlEx);
    }
    return null;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) UserPreference(com.github.games647.changeskin.core.model.UserPreference) SkinModel(com.github.games647.changeskin.core.model.skin.SkinModel)

Example 18 with SkinModel

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

the class SkinStorage method parseSkinData.

private SkinModel parseSkinData(ResultSet resultSet) throws SQLException {
    int skinId = resultSet.getInt(1);
    long timestamp = resultSet.getLong(2);
    UUID uuid = UUIDTypeAdapter.parseId(resultSet.getString(3));
    String name = resultSet.getString(4);
    boolean slimModel = resultSet.getBoolean(5);
    String skinUrl = resultSet.getString(6);
    String capeUrl = resultSet.getString(7);
    byte[] signature = resultSet.getBytes(8);
    return new SkinModel(skinId, timestamp, uuid, name, slimModel, skinUrl, capeUrl, signature);
}
Also used : UUID(java.util.UUID) SkinModel(com.github.games647.changeskin.core.model.skin.SkinModel)

Example 19 with SkinModel

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

the class SharedListener method refetchSkin.

protected boolean refetchSkin(String playerName, UserPreference preferences) {
    UUID ownerUUID = core.getUuidCache().get(playerName);
    if (ownerUUID == null && !core.getCrackedNames().containsKey(playerName)) {
        try {
            Optional<UUID> optUUID = core.getSkinApi().getUUID(playerName);
            if (optUUID.isPresent()) {
                ownerUUID = optUUID.get();
            }
        } catch (NotPremiumException ex) {
            core.getCrackedNames().put(playerName, new Object());
        } catch (RateLimitException ex) {
        // ignore
        }
    }
    if (ownerUUID != null) {
        core.getUuidCache().put(playerName, ownerUUID);
        SkinModel storedSkin = core.checkAutoUpdate(core.getStorage().getSkin(ownerUUID));
        if (storedSkin == null) {
            storedSkin = core.getSkinApi().downloadSkin(ownerUUID).orElse(null);
        }
        preferences.setTargetSkin(storedSkin);
        save(preferences);
        return true;
    }
    return false;
}
Also used : RateLimitException(com.github.games647.changeskin.core.RateLimitException) NotPremiumException(com.github.games647.changeskin.core.NotPremiumException) UUID(java.util.UUID) SkinModel(com.github.games647.changeskin.core.model.skin.SkinModel)

Example 20 with SkinModel

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

the class InfoCommand method execute.

@Override
public void execute(CommandSender sender, String[] strings) {
    if (!(sender instanceof ProxiedPlayer)) {
        plugin.sendMessage(sender, "no-console");
        return;
    }
    ProxiedPlayer player = (ProxiedPlayer) sender;
    UserPreference preference = plugin.getLoginSession((PendingConnection) player);
    Optional<SkinModel> optSkin = preference.getTargetSkin();
    if (optSkin.isPresent()) {
        String template = plugin.getCore().getMessage("skin-info");
        sender.sendMessage(TextComponent.fromLegacyText(formatter.apply(template, optSkin.get())));
    } else {
        plugin.sendMessage(sender, "skin-not-found");
    }
}
Also used : ProxiedPlayer(net.md_5.bungee.api.connection.ProxiedPlayer) UserPreference(com.github.games647.changeskin.core.model.UserPreference) SkinModel(com.github.games647.changeskin.core.model.skin.SkinModel)

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