Search in sources :

Example 11 with UserPreference

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

use of com.github.games647.changeskin.core.model.UserPreference 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 13 with UserPreference

use of com.github.games647.changeskin.core.model.UserPreference 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 14 with UserPreference

use of com.github.games647.changeskin.core.model.UserPreference 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 15 with UserPreference

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

Aggregations

UserPreference (com.github.games647.changeskin.core.model.UserPreference)17 SkinModel (com.github.games647.changeskin.core.model.skin.SkinModel)13 UUID (java.util.UUID)9 ProxiedPlayer (net.md_5.bungee.api.connection.ProxiedPlayer)5 Player (org.bukkit.entity.Player)4 EventHandler (net.md_5.bungee.event.EventHandler)3 Player (org.spongepowered.api.entity.living.player.Player)3 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 Server (net.md_5.bungee.api.connection.Server)2 EventHandler (org.bukkit.event.EventHandler)2 ChangeSkinBungee (com.github.games647.changeskin.bungee.ChangeSkinBungee)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 ChannelMessage (com.github.games647.changeskin.core.message.ChannelMessage)1 CheckPermMessage (com.github.games647.changeskin.core.message.CheckPermMessage)1 SharedListener (com.github.games647.changeskin.core.shared.SharedListener)1