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