Added player config related methods in IOffPlayer

This commit is contained in:
Marc Baloup 2021-09-12 17:38:52 +02:00
parent 218010e910
commit 904f5ea5d1
Signed by: marcbal
GPG Key ID: BBC0FE3ABC30B893
2 changed files with 123 additions and 0 deletions

View File

@ -299,4 +299,37 @@ public interface IOffPlayer {
/*
* Player config
*/
public default String getConfig(String key) throws DBException {
return SQLPlayerConfig.get(getUniqueId(), key);
}
public default String getConfig(String key, String deflt) throws DBException {
return SQLPlayerConfig.get(getUniqueId(), key, deflt);
}
public default void setConfig(String key, String value) throws DBException {
SQLPlayerConfig.set(getUniqueId(), key, value);
}
public default void unsetConfig(String key) throws DBException {
SQLPlayerConfig.unset(getUniqueId(), key);
}
public default boolean isWelcomeQuizzDone() {
try {
return Boolean.valueOf(getConfig("welcome.quizz.done", "false"));
} catch (DBException e) {
Log.severe("Error knowing if player has already done the quizz. Assuming they did for now.", e);
return true;
}
}
}

View File

@ -0,0 +1,90 @@
package fr.pandacube.lib.core.players;
import java.util.UUID;
import fr.pandacube.lib.core.db.DB;
import fr.pandacube.lib.core.db.DBException;
import fr.pandacube.lib.core.db.SQLElement;
import fr.pandacube.lib.core.db.SQLElementList;
import fr.pandacube.lib.core.db.SQLFKField;
import fr.pandacube.lib.core.db.SQLField;
public class SQLPlayerConfig extends SQLElement<SQLPlayerConfig> {
public SQLPlayerConfig() {
super();
}
public SQLPlayerConfig(int id) {
super(id);
}
/*
* Nom de la table
*/
@Override
protected String tableName() {
return "player_config";
}
/*
* Champs de la table
*/
public static final SQLFKField<SQLPlayerConfig, UUID, SQLPlayer> playerId = foreignKey(false, SQLPlayer.class, SQLPlayer.playerId);
public static final SQLField<SQLPlayerConfig, String> key = field(VARCHAR(255), false);
public static final SQLField<SQLPlayerConfig, String> value = field(VARCHAR(8192), false);
public static String get(UUID p, String k, String deflt) throws DBException {
SQLPlayerConfig res = DB.getFirst(SQLPlayerConfig.class, playerId.eq(p).and(key.eq(k)));
return res == null ? deflt : res.get(value);
}
public static String get(UUID p, String k) throws DBException {
return get(p, k, null);
}
public static void set(UUID p, String k, String v) throws DBException {
if (v == null) {
unset(p, k);
return;
}
SQLPlayerConfig entry = DB.getFirst(SQLPlayerConfig.class, playerId.eq(p).and(key.eq(k)));
if (entry == null) {
entry = new SQLPlayerConfig();
entry.set(playerId, p);
entry.set(key, k);
}
entry.set(value, v);
entry.save();
}
public static void unset(UUID p, String k) throws DBException {
SQLPlayerConfig entry = DB.getFirst(SQLPlayerConfig.class, playerId.eq(p).and(key.eq(k)));
if (entry != null)
entry.delete();
}
public static SQLElementList<SQLPlayerConfig> getAllFromPlayer(UUID p, String likeQuery) throws DBException {
return DB.getAll(SQLPlayerConfig.class, playerId.eq(p).and(key.like(likeQuery)));
}
public static SQLElementList<SQLPlayerConfig> getAllWithKeys(String likeQuery) throws DBException {
return DB.getAll(SQLPlayerConfig.class, key.like(likeQuery));
}
public static SQLElementList<SQLPlayerConfig> getAllWithKeyValue(String k, String v) throws DBException {
return DB.getAll(SQLPlayerConfig.class, key.eq(k).and(value.eq(v)));
}
}