Skip to content

Commit

Permalink
remove file removing in database flat & remove redundant variable in …
Browse files Browse the repository at this point in the history
…database sql (#1498)

* remove file removing in database flat & remove redundant variable in database sql

* added error notification

* typo fix
  • Loading branch information
Rollczi committed Apr 14, 2021
1 parent 908178c commit 5aa7664
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 59 deletions.
Expand Up @@ -52,7 +52,6 @@ public static void save(User user) {
statement.set("points", user.getRank().getPoints());
statement.set("kills", user.getRank().getKills());
statement.set("deaths", user.getRank().getDeaths());
statement.set("guild", user.hasGuild() ? "'" + user.getGuild().getName() + "'" : "");
statement.set("ban", user.isBanned() ? user.getBan().getBanTime() : 0);
statement.set("reason", (user.isBanned() ? user.getBan().getReason() : null));
statement.executeUpdate();
Expand Down
Expand Up @@ -46,7 +46,6 @@ public static void loadModels() {
tabUsers.add("points", SQLType.INT, true);
tabUsers.add("kills", SQLType.INT, true);
tabUsers.add("deaths", SQLType.INT, true);
tabUsers.add("guild", SQLType.VARCHAR, 100);
tabUsers.add("ban", SQLType.BIGINT);
tabUsers.add("reason", SQLType.TEXT);
tabUsers.setPrimaryKey("uuid");
Expand Down
140 changes: 83 additions & 57 deletions src/main/java/net/dzikoysk/funnyguilds/data/flat/FlatDataModel.java
Expand Up @@ -99,48 +99,57 @@ private void saveUsers(boolean ignoreNotChanged) {
return;
}

int errors = 0;

for (User user : UserUtils.getUsers()) {
if (user.getUUID() != null && user.getName() != null) {
if (ignoreNotChanged && !user.wasChanged()) {
continue;
}
if (user.getUUID() == null || user.getName() == null) {
errors++;
continue;
}

new FlatUser(user).serialize(this);
if (ignoreNotChanged && !user.wasChanged()) {
continue;
}

new FlatUser(user).serialize(this);
}

if (errors > 0) {
FunnyGuilds.getPluginLogger().error("Users save errors " + errors);
}
}

private void loadUsers() {
int repaired = 0;
File[] path = usersFolderFile.listFiles();
int errors = 0;

if (path != null) {
for (File file : path) {
if (file.isDirectory() || file.length() == 0) {
file.delete();
repaired++;
continue;
}
if (path == null) {
FunnyGuilds.getPluginLogger().error("critical error loading the users!");
return;
}

if (!UserUtils.validateUsername(StringUtils.removeEnd(file.getName(), ".yml"))) {
FunnyGuilds.getPluginLogger().warning("Skipping loading of user file '" + file.getName() + "'. Name is invalid.");
continue;
}
for (File file : path) {
if (file.length() == 0) {
continue;
}

User user = FlatUser.deserialize(file);
if (!UserUtils.validateUsername(StringUtils.removeEnd(file.getName(), ".yml"))) {
FunnyGuilds.getPluginLogger().warning("Skipping loading of user file '" + file.getName() + "'. Name is invalid.");
continue;
}

if (user == null) {
file.delete();
repaired++;
continue;
}
User user = FlatUser.deserialize(file);

user.wasChanged();
if (user == null) {
errors++;
continue;
}

user.wasChanged();
}

if (repaired > 0) {
FunnyGuilds.getPluginLogger().warning("Repaired conflicts: " + repaired);
if (errors > 0) {
FunnyGuilds.getPluginLogger().error("Users load errors " + errors);
}

FunnyGuilds.getPluginLogger().info("Loaded users: " + UserUtils.getUsers().size());
Expand All @@ -151,20 +160,20 @@ private void saveRegions(boolean ignoreNotChanged) {
return;
}

int defective = 0;
int errors = 0;

for (Region region : RegionUtils.getRegions()) {
if (ignoreNotChanged && !region.wasChanged()) {
continue;
}

if (!new FlatRegion(region).serialize(this)) {
RegionUtils.delete(region);
defective++;
errors++;
}
}

if (defective > 0) {
FunnyGuilds.getPluginLogger().warning("Deleted defective regions: " + defective);
if (errors > 0) {
FunnyGuilds.getPluginLogger().error("Regions save errors " + errors);
}
}

Expand All @@ -175,65 +184,82 @@ private void loadRegions() {
}

File[] path = regionsFolderFile.listFiles();
int errors = 0;

if (path == null) {
FunnyGuilds.getPluginLogger().error("critical error loading the regions!");
return;
}

if (path != null) {
for (File file : path) {
Region region = FlatRegion.deserialize(file);
if (region == null) {
file.delete();
}
else {
region.wasChanged();
RegionUtils.addRegion(region);
}
for (File file : path) {
Region region = FlatRegion.deserialize(file);

if (region == null) {
errors++;
continue;
}

region.wasChanged();
RegionUtils.addRegion(region);
}

if (errors > 0) {
FunnyGuilds.getPluginLogger().error("Guild load errors " + errors);
}

FunnyGuilds.getPluginLogger().info("Loaded regions: " + RegionUtils.getRegions().size());
}

private void saveGuilds(boolean ignoreNotChanged) {
int deleted = 0;
int errors = 0;

for (Guild guild : GuildUtils.getGuilds()) {
if (ignoreNotChanged && ! guild.wasChanged()) {
if (ignoreNotChanged && !guild.wasChanged()) {
continue;
}

if (! new FlatGuild(guild).serialize(this)) {
GuildUtils.deleteGuild(guild);
deleted++;
if (!new FlatGuild(guild).serialize(this)) {
errors++;
}
}

if (deleted > 0) {
FunnyGuilds.getPluginLogger().warning("Deleted defective guild: " + deleted);
if (errors > 0) {
FunnyGuilds.getPluginLogger().error("Guilds save errors: " + errors);
}
}

private void loadGuilds() {
GuildUtils.getGuilds().clear();
File[] path = guildsFolderFile.listFiles();
int errors = 0;

if (path != null) {
for (File file : path) {
Guild guild = FlatGuild.deserialize(file);
if (path == null) {
FunnyGuilds.getPluginLogger().error("critical error loading the guilds!");
return;
}

if (guild == null) {
file.delete();
continue;
}
for (File file : path) {
Guild guild = FlatGuild.deserialize(file);

guild.wasChanged();
if (guild == null) {
errors++;
continue;
}

guild.wasChanged();
}

for (Guild guild : GuildUtils.getGuilds()) {
if (guild.getOwner() != null) {
continue;
}

GuildUtils.deleteGuild(guild);
errors++;
FunnyGuilds.getPluginLogger().error("In guild " + guild.getTag() + " owner not exist!");
}

if (errors > 0) {
FunnyGuilds.getPluginLogger().error("Guild load errors " + errors);
}

ConcurrencyManager concurrencyManager = FunnyGuilds.getInstance().getConcurrencyManager();
Expand Down

0 comments on commit 5aa7664

Please sign in to comment.