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