Added default values for playerdata in config.yml

This means you can now set the starting amount of most points, etc.
without having to run commands on first player join!
This commit is contained in:
ASangarin 2020-10-01 16:30:40 +02:00
parent 529f805a1d
commit ed9c86942b
8 changed files with 276 additions and 220 deletions

View File

@ -44,10 +44,6 @@
<pattern>io.papermc.lib</pattern> <pattern>io.papermc.lib</pattern>
<shadedPattern>net.indyuce.mmocore.shaded.paperlib</shadedPattern> <shadedPattern>net.indyuce.mmocore.shaded.paperlib</shadedPattern>
</relocation> </relocation>
<relocation>
<pattern>com.github.jasync.sql.db</pattern>
<shadedPattern>eu.asangarin.sqltest</shadedPattern>
</relocation>
</relocations> </relocations>
</configuration> </configuration>
</plugin> </plugin>
@ -90,6 +86,12 @@
<version>7.0.2-SNAPSHOT</version> <version>7.0.2-SNAPSHOT</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
<dependency> <dependency>
<groupId>net.Indyuce</groupId> <groupId>net.Indyuce</groupId>
<artifactId>mmoitems</artifactId> <artifactId>mmoitems</artifactId>

View File

@ -195,5 +195,11 @@
<scope>system</scope> <scope>system</scope>
<systemPath>${basedir}/lib/PlaceholderAPI.jar</systemPath> <systemPath>${basedir}/lib/PlaceholderAPI.jar</systemPath>
</dependency> </dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -161,13 +161,16 @@ public class MMOCore extends JavaPlugin {
} }
public void onEnable() { public void onEnable() {
new SpigotPlugin(70575, this).checkForUpdate(); new SpigotPlugin(70575, this).checkForUpdate();
new Metrics(this); new Metrics(this);
saveDefaultConfig();
if (getConfig().contains("mysql") && getConfig().getBoolean("mysql.enabled")) if (getConfig().contains("mysql") && getConfig().getBoolean("mysql.enabled"))
dataProvider = new MySQLDataProvider(); dataProvider = new MySQLDataProvider();
if(getConfig().isConfigurationSection("default-playerdata"))
dataProvider.getDataManager().setDefaults(getConfig().getConfigurationSection("default-playerdata"));
if (Bukkit.getPluginManager().getPlugin("Vault") != null) if (Bukkit.getPluginManager().getPlugin("Vault") != null)
economy = new VaultEconomy(); economy = new VaultEconomy();
@ -247,7 +250,6 @@ public class MMOCore extends JavaPlugin {
return; return;
} }
saveDefaultConfig();
reloadPlugin(); reloadPlugin();
if (getConfig().getBoolean("vanilla-exp-redirection.enabled")) if (getConfig().getBoolean("vanilla-exp-redirection.enabled"))

View File

@ -6,8 +6,10 @@ import java.util.stream.Collectors;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import lombok.Getter;
import net.Indyuce.mmocore.MMOCore; import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.event.PlayerDataLoadEvent; import net.Indyuce.mmocore.api.event.PlayerDataLoadEvent;
import net.Indyuce.mmocore.api.player.OfflinePlayerData; import net.Indyuce.mmocore.api.player.OfflinePlayerData;
@ -16,6 +18,7 @@ import net.mmogroup.mmolib.api.player.MMOPlayerData;
public abstract class PlayerDataManager { public abstract class PlayerDataManager {
// private final Map<UUID, PlayerData> map = new HashMap<>(); // private final Map<UUID, PlayerData> map = new HashMap<>();
private DefaultPlayerData defaults = new DefaultPlayerData();
public PlayerData get(OfflinePlayer player) { public PlayerData get(OfflinePlayer player) {
return get(player.getUniqueId()); return get(player.getUniqueId());
@ -48,7 +51,7 @@ public abstract class PlayerDataManager {
* calls the load event on the MAIN thread * calls the load event on the MAIN thread
*/ */
Bukkit.getScheduler().runTaskAsynchronously(MMOCore.plugin, () -> { Bukkit.getScheduler().runTaskAsynchronously(MMOCore.plugin, () -> {
loadData(generated); loadData(generated, defaults);
Bukkit.getScheduler().runTask(MMOCore.plugin, () -> Bukkit.getPluginManager().callEvent(new PlayerDataLoadEvent(generated))); Bukkit.getScheduler().runTask(MMOCore.plugin, () -> Bukkit.getPluginManager().callEvent(new PlayerDataLoadEvent(generated)));
generated.getStats().updateStats(); generated.getStats().updateStats();
}); });
@ -63,9 +66,35 @@ public abstract class PlayerDataManager {
return MMOPlayerData.getLoaded().stream().filter(data -> data.getMMOCore() != null).map(data -> data.getMMOCore()).collect(Collectors.toSet()); return MMOPlayerData.getLoaded().stream().filter(data -> data.getMMOCore() != null).map(data -> data.getMMOCore()).collect(Collectors.toSet());
} }
public abstract void loadData(PlayerData data); public abstract void loadData(PlayerData data, DefaultPlayerData defaults);
public abstract void saveData(PlayerData data); public abstract void saveData(PlayerData data);
public abstract void remove(PlayerData data); public abstract void remove(PlayerData data);
public void setDefaults(ConfigurationSection config) {
defaults = new DefaultPlayerData(config);
}
@Getter
public class DefaultPlayerData {
private final int level, classPoints, skillPoints,
attributePoints, attrReallocPoints;
public DefaultPlayerData(ConfigurationSection config) {
level = config.getInt("level", 1);
classPoints = config.getInt("class-points");
skillPoints = config.getInt("skill-points");
attributePoints = config.getInt("attribute-points");
attrReallocPoints = config.getInt("attribute-realloc-points");
}
public DefaultPlayerData() {
level = 1;
classPoints = 0;
skillPoints = 0;
attributePoints = 0;
attrReallocPoints = 0;
}
}
} }

View File

@ -2,6 +2,7 @@ package net.Indyuce.mmocore.manager.data.mysql;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
@ -27,7 +28,7 @@ public class MySQLDataProvider implements DataProvider {
config = new MySQLConfig(MMOCore.plugin.getConfig().getConfigurationSection("mysql")); config = new MySQLConfig(MMOCore.plugin.getConfig().getConfigurationSection("mysql"));
connection = MySQLConnectionBuilder.createConnectionPool(config.getConnectionString()); connection = MySQLConnectionBuilder.createConnectionPool(config.getConnectionString());
executeUpdate("CREATE TABLE IF NOT EXISTS mmocore_playerdata (uuid VARCHAR(36),class_points INT(11) DEFAULT 0,skill_points INT(11) DEFAULT 0,attribute_points INT(11) DEFAULT 0,attribute_realloc_points INT(11) DEFAULT 0,level INT(11) DEFAULT 0,experience INT(11) DEFAULT 0,class VARCHAR(20),guild VARCHAR(20),last_login LONG,attributes JSON,professions JSON,quests JSON,waypoints JSON,friends JSON,skills JSON,bound_skills JSON,class_info JSON,PRIMARY KEY (uuid));"); executeUpdate("CREATE TABLE IF NOT EXISTS mmocore_playerdata (uuid VARCHAR(36),class_points INT(11) DEFAULT 0,skill_points INT(11) DEFAULT 0,attribute_points INT(11) DEFAULT 0,attribute_realloc_points INT(11) DEFAULT 0,level INT(11) DEFAULT 1,experience INT(11) DEFAULT 0,class VARCHAR(20),guild VARCHAR(20),last_login LONG,attributes JSON,professions JSON,quests JSON,waypoints JSON,friends JSON,skills JSON,bound_skills JSON,class_info JSON,PRIMARY KEY (uuid));");
} }
public ResultSet getResult(String sql) { public ResultSet getResult(String sql) {
@ -35,6 +36,7 @@ public class MySQLDataProvider implements DataProvider {
CompletableFuture<QueryResult> future = connection.sendPreparedStatement(sql); CompletableFuture<QueryResult> future = connection.sendPreparedStatement(sql);
return future.get().getRows(); return future.get().getRows();
} catch (InterruptedException | ExecutionException e) { } catch (InterruptedException | ExecutionException e) {
MMOCore.log(Level.SEVERE, "MySQL Operation Failed!");
e.printStackTrace(); e.printStackTrace();
return null; return null;
} }
@ -44,6 +46,7 @@ public class MySQLDataProvider implements DataProvider {
try { try {
connection.sendPreparedStatement(sql).get(); connection.sendPreparedStatement(sql).get();
} catch (InterruptedException | ExecutionException e) { } catch (InterruptedException | ExecutionException e) {
MMOCore.log(Level.SEVERE, "MySQL Operation Failed!");
e.printStackTrace(); e.printStackTrace();
} }
} }

View File

@ -33,18 +33,25 @@ public class MySQLPlayerDataManager extends PlayerDataManager {
} }
@Override @Override
public void loadData(PlayerData data) { public void loadData(PlayerData data, DefaultPlayerData defaults) {
ResultSet result = provider ResultSet result = provider
.getResult("SELECT * FROM mmocore_playerdata WHERE uuid = '" + data.getUniqueId() + "';"); .getResult("SELECT * FROM mmocore_playerdata WHERE uuid = '" + data.getUniqueId() + "';");
if (result == null) {
MMOCore.log(Level.SEVERE,
"Failed to load playerdata of '" + data.getPlayer().getName() + "' from MySQL server");
return;
}
// player data not initialized yet // player data not initialized yet
if (result.size() < 1) if (result.size() < 1) {
data.setLevel(defaults.getLevel());
data.setClassPoints(defaults.getClassPoints());
data.setSkillPoints(defaults.getSkillPoints());
data.setAttributePoints(defaults.getAttributePoints());
data.setAttributeReallocationPoints(defaults.getAttrReallocPoints());
data.setExperience(0);
data.setMana(data.getStats().getStat(StatType.MAX_MANA));
data.setStamina(data.getStats().getStat(StatType.MAX_STAMINA));
data.setStellium(data.getStats().getStat(StatType.MAX_STELLIUM));
data.getQuestData().updateBossBar();
return; return;
}
RowData row = result.get(0); RowData row = result.get(0);
@ -66,9 +73,8 @@ public class MySQLPlayerDataManager extends PlayerDataManager {
data.getAttributes().load(row.getString("attributes")); data.getAttributes().load(row.getString("attributes"));
if (!isEmpty(row.getString("professions"))) if (!isEmpty(row.getString("professions")))
data.getCollectionSkills().load(row.getString("professions")); data.getCollectionSkills().load(row.getString("professions"));
String quests = row.getString("quests"); if (!isEmpty(row.getString("quests")))
if (!isEmpty(quests)) data.getQuestData().load(row.getString("quests"));
data.getQuestData().load(quests);
data.getQuestData().updateBossBar(); data.getQuestData().updateBossBar();
if (!isEmpty(row.getString("waypoints"))) if (!isEmpty(row.getString("waypoints")))
data.getWaypoints().addAll(getJSONArray(row.getString("waypoints"))); data.getWaypoints().addAll(getJSONArray(row.getString("waypoints")));
@ -191,10 +197,9 @@ public class MySQLPlayerDataManager extends PlayerDataManager {
level = row.getInt("level"); level = row.getInt("level");
lastLogin = row.getLong("last_login"); lastLogin = row.getLong("last_login");
profess = row.getString("class").equalsIgnoreCase("null") profess = isEmpty(row.getString("class")) ? MMOCore.plugin.classManager.getDefaultClass()
? MMOCore.plugin.classManager.getDefaultClass()
: MMOCore.plugin.classManager.get(row.getString("class")); : MMOCore.plugin.classManager.get(row.getString("class"));
if (!row.getString("friends").equalsIgnoreCase("null")) if (!isEmpty(row.getString("friends")))
getJSONArray(row.getString("friends")).forEach(str -> friends.add(UUID.fromString(str))); getJSONArray(row.getString("friends")).forEach(str -> friends.add(UUID.fromString(str)));
else else
friends = new ArrayList<UUID>(); friends = new ArrayList<UUID>();

View File

@ -21,14 +21,14 @@ import net.Indyuce.mmocore.manager.data.PlayerDataManager;
public class YAMLPlayerDataManager extends PlayerDataManager { public class YAMLPlayerDataManager extends PlayerDataManager {
@Override @Override
public void loadData(PlayerData data) { public void loadData(PlayerData data, DefaultPlayerData defaults) {
FileConfiguration config = new ConfigFile(data.getPlayer()).getConfig(); FileConfiguration config = new ConfigFile(data.getPlayer()).getConfig();
data.setClassPoints(config.getInt("class-points")); data.setClassPoints(config.getInt("class-points", defaults.getClassPoints()));
data.setSkillPoints(config.getInt("skill-points")); data.setSkillPoints(config.getInt("skill-points", defaults.getSkillPoints()));
data.setAttributePoints(config.getInt("attribute-points")); data.setAttributePoints(config.getInt("attribute-points", defaults.getAttributePoints()));
data.setAttributeReallocationPoints(config.getInt("attribute-realloc-points")); data.setAttributeReallocationPoints(config.getInt("attribute-realloc-points", defaults.getAttrReallocPoints()));
data.setLevel(config.getInt("level")); data.setLevel(config.getInt("level", defaults.getLevel()));
data.setExperience(config.getInt("experience")); data.setExperience(config.getInt("experience"));
if (config.contains("class")) if (config.contains("class"))
data.setClass(MMOCore.plugin.classManager.get(config.getString("class"))); data.setClass(MMOCore.plugin.classManager.get(config.getString("class")));

View File

@ -14,6 +14,15 @@ mysql:
user: mmolover user: mmolover
pass: ILoveAria pass: ILoveAria
# The default values for all playerdata
# All new players will start with these values
default-playerdata:
level: 1
class-points: 0
skill-points: 0
attribute-points: 0
attribute-realloc-points: 0
# The list of all conditions which must be met for the # The list of all conditions which must be met for the
# BLOCK REGEN and BLOCK RESTRICTIONS to apply. Set to # BLOCK REGEN and BLOCK RESTRICTIONS to apply. Set to
# 'custom-mine-conditions: []' to disable custom mining entirely. # 'custom-mine-conditions: []' to disable custom mining entirely.