Compare commits

...

2 commits

Author SHA1 Message Date
666c45735d
fix(favorites): wrong values when item create 2025-06-08 09:16:39 +02:00
7cacb36fec
feat(storage): favorites 2025-06-08 09:11:45 +02:00
3 changed files with 26 additions and 8 deletions

View file

@ -33,13 +33,14 @@ public class FavoriteAppearanceEntryItem extends AsyncItem {
}, (_ -> true)).getItemProvider(),
() -> {
try {
final String name = (appearance.name() == null ? appearance.skin() : appearance.name());
final String skin = (appearance.skin() == null ? appearance.name() : appearance.skin());
// TODO (Ineanto, 08/06/2025): set a default skin if the entry contains only a name
final String name = (appearance.name() == null ? "N/A" : appearance.name());
final String skin = (appearance.skin() == null ? "N/A" : appearance.skin());
final SkullBuilder skull = new SkullBuilder(skin);
return playerLanguage.translateItem(skull, LanguageKey.GUI.Admin.Cache.ENTRY, name);
} catch (MojangApiUtils.MojangApiException | IOException e) {
Nicko.getInstance().getLogger().warning("Unable to get Head texture for specified UUID (" + appearance.skin() + ")! (GUI/Favorites/Entry)");
return ItemDefaults.getErrorSkullItem(playerLanguage, LanguageKey.GUI.Admin.Cache.ENTRY, Nicko.getInstance().getMojangAPI().getUUIDName("Notch"));
return ItemDefaults.getErrorSkullItem(playerLanguage, LanguageKey.GUI.Admin.Cache.ENTRY, "...");
}
});
this.playerLanguage = playerLanguage;

View file

@ -1,5 +1,8 @@
package xyz.ineanto.nicko.storage.mariadb;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import xyz.ineanto.nicko.appearance.ActionResult;
import xyz.ineanto.nicko.appearance.Appearance;
import xyz.ineanto.nicko.config.Configuration;
@ -12,6 +15,7 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.logging.Logger;
@ -19,6 +23,7 @@ import java.util.logging.Logger;
public class MariaDBStorage extends Storage {
private final Logger logger = Logger.getLogger("SQLStorage");
private final Configuration configuration;
private final Gson gson = new GsonBuilder().serializeNulls().create();
private MariaDBStorageProvider provider;
@ -86,15 +91,17 @@ public class MariaDBStorage extends Storage {
String skin = "";
String locale = "";
boolean randomSkin = false;
List<Appearance> favorites = Collections.emptyList();
while (resultSet.next()) {
name = resultSet.getString("name");
skin = resultSet.getString("skin");
locale = resultSet.getString("locale");
randomSkin = resultSet.getBoolean("randomskin");
favorites = gson.fromJson(resultSet.getString("favorites"), new TypeToken<List<Appearance>>() { }.getType());
}
// TODO (Ineanto, 17/05/2025): Retrieve favorites
final NickoProfile profile = new NickoProfile(new Appearance(name, skin), Language.fromCode(locale), randomSkin, Collections.emptyList());
final NickoProfile profile = new NickoProfile(new Appearance(name, skin), Language.fromCode(locale), randomSkin, favorites);
return Optional.of(profile);
} catch (SQLException e) {
logger.warning("Couldn't fetch profile: " + e.getMessage());
@ -120,13 +127,14 @@ public class MariaDBStorage extends Storage {
}
private PreparedStatement getInsertStatement(Connection connection, UUID uuid, NickoProfile profile) throws SQLException {
final String sql = "INSERT IGNORE INTO nicko.DATA (`uuid`, `name`, `skin`, `locale`, `randomskin`) VALUES (?, ?, ?, ?, ?)";
final String sql = "INSERT IGNORE INTO nicko.DATA (`uuid`, `name`, `skin`, `locale`, `randomskin`, `favorites`) VALUES (?, ?, ?, ?, ?, ?)";
final PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, uuid.toString());
statement.setString(2, profile.getName() == null ? null : profile.getName());
statement.setString(3, profile.getSkin() == null ? null : profile.getSkin());
statement.setString(4, profile.getLocale().getCode());
statement.setBoolean(5, profile.isRandomSkin());
statement.setString(6, gson.toJson(profile.getFavorites(), new TypeToken<List<Appearance>>() { }.getRawType()));
return statement;
}

View file

@ -1,5 +1,8 @@
package xyz.ineanto.nicko.storage.mysql;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import xyz.ineanto.nicko.appearance.ActionResult;
import xyz.ineanto.nicko.appearance.Appearance;
import xyz.ineanto.nicko.config.Configuration;
@ -12,6 +15,7 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.logging.Logger;
@ -19,6 +23,7 @@ import java.util.logging.Logger;
public class MySQLStorage extends Storage {
private final Logger logger = Logger.getLogger("SQLStorage");
private final Configuration configuration;
private final Gson gson = new GsonBuilder().serializeNulls().create();
private MySQLStorageProvider provider;
@ -86,15 +91,16 @@ public class MySQLStorage extends Storage {
String skin = "";
String locale = "";
boolean randomSkin = false;
List<Appearance> favorites = Collections.emptyList();
while (resultSet.next()) {
name = resultSet.getString("name");
skin = resultSet.getString("skin");
locale = resultSet.getString("locale");
randomSkin = resultSet.getBoolean("randomskin");
favorites = gson.fromJson(resultSet.getString("favorites"), new TypeToken<List<Appearance>>() { }.getType());
}
// TODO (Ineanto, 17/05/2025): Retrieve favorites
final NickoProfile profile = new NickoProfile(new Appearance(name, skin), Language.fromCode(locale), randomSkin, Collections.emptyList());
final NickoProfile profile = new NickoProfile(new Appearance(name, skin), Language.fromCode(locale), randomSkin, favorites);
return Optional.of(profile);
} catch (SQLException e) {
logger.warning("Couldn't fetch profile: " + e.getMessage());
@ -120,13 +126,16 @@ public class MySQLStorage extends Storage {
}
private PreparedStatement getInsertStatement(Connection connection, UUID uuid, NickoProfile profile) throws SQLException {
final String sql = "INSERT IGNORE INTO nicko.DATA (`uuid`, `name`, `skin`, `locale`, `randomskin`) VALUES (?, ?, ?, ?, ?)";
final String sql = "INSERT IGNORE INTO nicko.DATA (`uuid`, `name`, `skin`, `locale`, `randomskin`, `favorites`) VALUES (?, ?, ?, ?, ?, ?)";
final PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, uuid.toString());
statement.setString(2, profile.getName() == null ? null : profile.getName());
statement.setString(3, profile.getSkin() == null ? null : profile.getSkin());
statement.setString(4, profile.getLocale().getCode());
statement.setBoolean(5, profile.isRandomSkin());
// Ineanto, 08/06/2025: this will never fucking work
statement.setString(6, gson.toJson(profile.getFavorites(), new TypeToken<List<Appearance>>() { }.getRawType()));
return statement;
}