Search in sources :

Example 26 with Skin

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;
}
Also used : TextureModel(com.github.games647.changeskin.core.model.skin.TextureModel) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 27 with Skin

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());
}
Also used : Player(org.bukkit.entity.Player) UserPreference(com.github.games647.changeskin.core.model.UserPreference) EventHandler(org.bukkit.event.EventHandler)

Example 28 with Skin

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();
}
Also used : SkinProperty(com.github.games647.changeskin.core.model.skin.SkinProperty) TexturesModel(com.github.games647.changeskin.core.model.skin.TexturesModel) IOException(java.io.IOException)

Example 29 with Skin

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");
    }
}
Also used : ProxiedPlayer(net.md_5.bungee.api.connection.ProxiedPlayer) UserPreference(com.github.games647.changeskin.core.model.UserPreference) SkinModel(com.github.games647.changeskin.core.model.skin.SkinModel)

Example 30 with Skin

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");
    }
}
Also used : SkinDownloader(com.github.games647.changeskin.bukkit.task.SkinDownloader) UUID(java.util.UUID)

Aggregations

SkinModel (com.github.games647.changeskin.core.model.skin.SkinModel)11 UserPreference (com.github.games647.changeskin.core.model.UserPreference)10 UUID (java.util.UUID)8 Skin (com.github.games647.craftapi.model.skin.Skin)7 SkinProperty (com.github.games647.craftapi.model.skin.SkinProperty)6 SkinPropertyTest (com.github.games647.craftapi.model.skin.SkinPropertyTest)6 Test (org.junit.Test)6 ProxiedPlayer (net.md_5.bungee.api.connection.ProxiedPlayer)5 Texture (com.github.games647.craftapi.model.skin.Texture)3 EventHandler (net.md_5.bungee.event.EventHandler)3 SkinProperty (com.github.games647.changeskin.core.model.skin.SkinProperty)2 TextureModel (com.github.games647.changeskin.core.model.skin.TextureModel)2 TextureType (com.github.games647.changeskin.core.model.skin.TextureType)2 TexturesModel (com.github.games647.changeskin.core.model.skin.TexturesModel)2 IOException (java.io.IOException)2 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 Optional (java.util.Optional)2