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