use of com.github.games647.craftapi.model.skin.Skin 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();
slimModel = skinTexture.isSlim();
}
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.craftapi.model.skin.Skin in project ChangeSkin by games647.
the class LoginListener method onPlayerLogin.
@EventHandler(priority = EventPriority.HIGHEST)
public void onPlayerLogin(PlayerLoginEvent loginEvent) {
if (loginEvent.getResult() != PlayerLoginEvent.Result.ALLOWED) {
// in this event isCancelled option in the annotation doesn't work
return;
}
Player player = loginEvent.getPlayer();
// updates to the chosen one
UserPreference preferences = plugin.getLoginSession(player.getUniqueId());
if (preferences != null) {
preferences.getTargetSkin().ifPresent(skin -> plugin.getApi().applySkin(player, skin));
}
plugin.endSession(player.getUniqueId());
}
use of com.github.games647.craftapi.model.skin.Skin in project ChangeSkin by games647.
the class MojangSkinApi method parseSkinTexture.
private Optional<SkinModel> parseSkinTexture(Reader reader) {
TexturesModel texturesModel = gson.fromJson(reader, TexturesModel.class);
SkinProperty[] properties = texturesModel.getProperties();
try {
if (properties != null && properties.length > 0) {
SkinProperty propertiesModel = properties[0];
// base64 encoded skin data
String encodedSkin = propertiesModel.getValue();
String signature = propertiesModel.getSignature();
return Optional.of(SkinModel.createSkinFromEncoded(encodedSkin, signature));
}
} catch (Exception ex) {
logger.error("Failed to parse skin model", ex);
logger.error(texturesModel.toString());
}
return Optional.empty();
}
use of com.github.games647.craftapi.model.skin.Skin 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(player.getPendingConnection());
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");
}
}
use of com.github.games647.craftapi.model.skin.Skin in project ChangeSkin by games647.
the class SetCommand method setSkinUUID.
private void setSkinUUID(CommandSender sender, Player receiverPayer, String targetUUID, boolean keepSkin) {
try {
UUID uuid = UUID.fromString(targetUUID);
if (plugin.getConfig().getBoolean("skinPermission") && !plugin.hasSkinPermission(sender, uuid, true)) {
return;
}
plugin.sendMessage(sender, "skin-change-queue");
Runnable skinDownloader = new SkinDownloader(plugin, sender, receiverPayer, uuid, keepSkin);
Bukkit.getScheduler().runTaskAsynchronously(plugin, skinDownloader);
} catch (IllegalArgumentException illegalArgumentException) {
plugin.sendMessage(sender, "invalid-uuid");
}
}
Aggregations