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();
}
}
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");
}
}
}
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);
}
}
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;
}
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;
}
Aggregations