Search in sources :

Example 16 with Skin

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

Example 17 with Skin

use of com.github.games647.craftapi.model.skin.Skin in project ChangeSkin by games647.

the class SharedInvalidator method run.

@Override
public void run() {
    UserPreference preferences = core.getStorage().getPreferences(receiverUUID);
    Optional<SkinModel> ownedSkin = preferences.getTargetSkin();
    if (ownedSkin.isPresent()) {
        sendMessageInvoker("invalidate-request");
        core.getSkinApi().downloadSkin(ownedSkin.get().getProfileId()).ifPresent(this::scheduleApplyTask);
    } else {
        sendMessageInvoker("dont-have-skin");
    }
}
Also used : UserPreference(com.github.games647.changeskin.core.model.UserPreference) SkinModel(com.github.games647.changeskin.core.model.skin.SkinModel)

Example 18 with Skin

use of com.github.games647.craftapi.model.skin.Skin in project ChangeSkin by games647.

the class SharedSkinSelect method run.

@Override
public void run() {
    SkinModel targetSkin = core.getStorage().getSkin(targetId);
    if (targetSkin == null) {
        sendMessageInvoker("skin-not-found");
        return;
    }
    scheduleApplyTask(targetSkin);
}
Also used : SkinModel(com.github.games647.changeskin.core.model.skin.SkinModel)

Example 19 with Skin

use of com.github.games647.craftapi.model.skin.Skin in project FastLogin by games647.

the class ConnectListener method onLogin.

@EventHandler(priority = EventPriority.LOWEST)
public void onLogin(LoginEvent loginEvent) {
    if (loginEvent.isCancelled()) {
        return;
    }
    // use the login event instead of the post login event in order to send the login success packet to the client
    // with the offline uuid this makes it possible to set the skin then
    PendingConnection connection = loginEvent.getConnection();
    if (connection.isOnlineMode()) {
        LoginSession session = plugin.getSession().get(connection);
        UUID verifiedUUID = connection.getUniqueId();
        String verifiedUsername = connection.getName();
        session.setUuid(verifiedUUID);
        session.setVerifiedUsername(verifiedUsername);
        StoredProfile playerProfile = session.getProfile();
        playerProfile.setId(verifiedUUID);
        // bungeecord will do this automatically so override it on disabled option
        if (uniqueIdSetter != null) {
            InitialHandler initialHandler = (InitialHandler) connection;
            if (!plugin.getCore().getConfig().get("premiumUuid", true)) {
                setOfflineId(initialHandler, verifiedUsername);
            }
            if (!plugin.getCore().getConfig().get("forwardSkin", true)) {
                // this is null on offline mode
                LoginResult loginProfile = initialHandler.getLoginProfile();
                loginProfile.setProperties(emptyProperties);
            }
        }
    }
}
Also used : BungeeLoginSession(com.github.games647.fastlogin.bungee.BungeeLoginSession) LoginSession(com.github.games647.fastlogin.core.shared.LoginSession) StoredProfile(com.github.games647.fastlogin.core.StoredProfile) LoginResult(net.md_5.bungee.connection.LoginResult) PendingConnection(net.md_5.bungee.api.connection.PendingConnection) UUID(java.util.UUID) InitialHandler(net.md_5.bungee.connection.InitialHandler) EventHandler(net.md_5.bungee.event.EventHandler)

Example 20 with Skin

use of com.github.games647.craftapi.model.skin.Skin in project ChangeSkin by games647.

the class MessageListener method onPermissionSuccess.

private void onPermissionSuccess(PermResultMessage message, ProxiedPlayer invoker) {
    SkinModel targetSkin = message.getSkin();
    UUID receiverUUID = message.getReceiverUUID();
    ProxiedPlayer receiver = ProxyServer.getInstance().getPlayer(receiverUUID);
    if (receiver == null || !receiver.isConnected()) {
        // receiver is not online cancel
        return;
    }
    // add cooldown
    core.getCooldownService().trackPlayer(invoker.getUniqueId());
    // Save the target uuid from the requesting player source
    final UserPreference preferences = core.getStorage().getPreferences(receiver.getUniqueId());
    preferences.setTargetSkin(targetSkin);
    ProxyServer.getInstance().getScheduler().runAsync(plugin, () -> {
        if (core.getStorage().save(targetSkin)) {
            core.getStorage().save(preferences);
        }
    });
    if (core.getConfig().getBoolean("instantSkinChange")) {
        plugin.getApi().applySkin(receiver, targetSkin);
        plugin.sendMessage(invoker, "skin-changed");
    } else {
        plugin.sendMessage(invoker, "skin-changed-no-instant");
    }
}
Also used : ProxiedPlayer(net.md_5.bungee.api.connection.ProxiedPlayer) UserPreference(com.github.games647.changeskin.core.model.UserPreference) UUID(java.util.UUID) SkinModel(com.github.games647.changeskin.core.model.skin.SkinModel)

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