summaryrefslogtreecommitdiffstats
path: root/src/main/java
diff options
context:
space:
mode:
authorLMBishop <13875753+LMBishop@users.noreply.github.com>2021-06-05 14:49:21 +0100
committerLMBishop <13875753+LMBishop@users.noreply.github.com>2021-06-05 14:49:21 +0100
commit159109f5c28e8611171ce0d492393ccd3382231e (patch)
treeb9dc40c58ac6c3b676e6a4bc9378ccb96367c7fe /src/main/java
parent0e73490abce721bd9a15dc3b16d016ff7823b3cf (diff)
Reimplement modified status
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/com/leonardobishop/quests/Quests.java2
-rw-r--r--src/main/java/com/leonardobishop/quests/QuestsAutosaveRunnable.java3
-rw-r--r--src/main/java/com/leonardobishop/quests/command/QuestsCommand.java8
-rw-r--r--src/main/java/com/leonardobishop/quests/listener/PlayerLeaveListener.java3
-rw-r--r--src/main/java/com/leonardobishop/quests/player/QPlayerManager.java54
-rw-r--r--src/main/java/com/leonardobishop/quests/player/questprogressfile/QuestProgress.java5
-rw-r--r--src/main/java/com/leonardobishop/quests/player/questprogressfile/QuestProgressFile.java6
-rw-r--r--src/main/java/com/leonardobishop/quests/player/questprogressfile/TaskProgress.java4
-rw-r--r--src/main/java/com/leonardobishop/quests/storage/MySqlStorageProvider.java9
9 files changed, 61 insertions, 33 deletions
diff --git a/src/main/java/com/leonardobishop/quests/Quests.java b/src/main/java/com/leonardobishop/quests/Quests.java
index 5400c265..21cd8a6e 100644
--- a/src/main/java/com/leonardobishop/quests/Quests.java
+++ b/src/main/java/com/leonardobishop/quests/Quests.java
@@ -303,7 +303,7 @@ public class Quests extends JavaPlugin {
}
for (QPlayer qPlayer : qPlayerManager.getQPlayers()) {
try {
- qPlayerManager.savePlayer(qPlayer.getPlayerUUID());
+ qPlayerManager.savePlayerSync(qPlayer.getPlayerUUID());
} catch (Exception ignored) { }
}
if (placeholderAPIHook != null) placeholderAPIHook.unregisterExpansion();
diff --git a/src/main/java/com/leonardobishop/quests/QuestsAutosaveRunnable.java b/src/main/java/com/leonardobishop/quests/QuestsAutosaveRunnable.java
index c042fd32..eaa37d87 100644
--- a/src/main/java/com/leonardobishop/quests/QuestsAutosaveRunnable.java
+++ b/src/main/java/com/leonardobishop/quests/QuestsAutosaveRunnable.java
@@ -37,8 +37,7 @@ public class QuestsAutosaveRunnable extends BukkitRunnable {
}
if (Bukkit.getPlayer(player) != null) {
- QuestProgressFile clonedProgressFile = new QuestProgressFile(plugin.getPlayerManager().getPlayer(player).getQuestProgressFile());
- Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> plugin.getPlayerManager().savePlayer(player, clonedProgressFile));
+ plugin.getPlayerManager().savePlayer(player);
}
}
diff --git a/src/main/java/com/leonardobishop/quests/command/QuestsCommand.java b/src/main/java/com/leonardobishop/quests/command/QuestsCommand.java
index 47aa050e..d33baa65 100644
--- a/src/main/java/com/leonardobishop/quests/command/QuestsCommand.java
+++ b/src/main/java/com/leonardobishop/quests/command/QuestsCommand.java
@@ -285,7 +285,7 @@ public class QuestsCommand implements TabExecutor {
}
QuestProgressFile questProgressFile = qPlayer.getQuestProgressFile();
questProgressFile.clear();
- plugin.getPlayerManager().savePlayer(uuid, questProgressFile);
+ plugin.getPlayerManager().savePlayerSync(uuid, questProgressFile);
if (Bukkit.getPlayer(uuid) == null) {
plugin.getPlayerManager().dropPlayer(uuid);
}
@@ -355,7 +355,7 @@ public class QuestsCommand implements TabExecutor {
}
if (args[2].equalsIgnoreCase("reset")) {
questProgressFile.generateBlankQuestProgress(quest);
- plugin.getPlayerManager().savePlayer(uuid, questProgressFile);
+ plugin.getPlayerManager().savePlayerSync(uuid, questProgressFile);
sender.sendMessage(Messages.COMMAND_QUEST_ADMIN_RESET_SUCCESS.getMessage().replace("{player}", name).replace("{quest}", quest.getId()));
success = true;
} else if (args[2].equalsIgnoreCase("start")) {
@@ -382,12 +382,12 @@ public class QuestsCommand implements TabExecutor {
sender.sendMessage(Messages.COMMAND_QUEST_ADMIN_START_FAILCATEGORYPERMISSION.getMessage().replace("{player}", name).replace("{quest}", quest.getId()));
return true;
}
- plugin.getPlayerManager().savePlayer(uuid, questProgressFile);
+ plugin.getPlayerManager().savePlayerSync(uuid, questProgressFile);
sender.sendMessage(Messages.COMMAND_QUEST_ADMIN_START_SUCCESS.getMessage().replace("{player}", name).replace("{quest}", quest.getId()));
success = true;
} else if (args[2].equalsIgnoreCase("complete")) {
qPlayer.completeQuest(quest);
- plugin.getPlayerManager().savePlayer(uuid, questProgressFile);
+ plugin.getPlayerManager().savePlayerSync(uuid, questProgressFile);
sender.sendMessage(Messages.COMMAND_QUEST_ADMIN_COMPLETE_SUCCESS.getMessage().replace("{player}", name).replace("{quest}", quest.getId()));
success = true;
}
diff --git a/src/main/java/com/leonardobishop/quests/listener/PlayerLeaveListener.java b/src/main/java/com/leonardobishop/quests/listener/PlayerLeaveListener.java
index 99cad4e2..d246e1f6 100644
--- a/src/main/java/com/leonardobishop/quests/listener/PlayerLeaveListener.java
+++ b/src/main/java/com/leonardobishop/quests/listener/PlayerLeaveListener.java
@@ -20,8 +20,7 @@ public class PlayerLeaveListener implements Listener {
public void onEvent(PlayerQuitEvent event) {
QPlayer qPlayer = plugin.getPlayerManager().getPlayer(event.getPlayer().getUniqueId());
if (qPlayer == null) return;
- QuestProgressFile clonedProgressFile = new QuestProgressFile(qPlayer.getQuestProgressFile());
- Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> plugin.getPlayerManager().removePlayer(qPlayer.getPlayerUUID(), clonedProgressFile));
+ plugin.getPlayerManager().removePlayer(qPlayer.getPlayerUUID());
}
}
diff --git a/src/main/java/com/leonardobishop/quests/player/QPlayerManager.java b/src/main/java/com/leonardobishop/quests/player/QPlayerManager.java
index 0509799f..2cd9cbfd 100644
--- a/src/main/java/com/leonardobishop/quests/player/QPlayerManager.java
+++ b/src/main/java/com/leonardobishop/quests/player/QPlayerManager.java
@@ -59,37 +59,65 @@ public class QPlayerManager {
}
/**
- * Unloads and saves the player to disk.
+ * Unloads and schedules a save for the player. See {@link QPlayerManager#savePlayer(UUID)}
*
* @param uuid the uuid of the player
- * @param questProgressFile the quest progress file to save
*/
- public void removePlayer(UUID uuid, QuestProgressFile questProgressFile) {
+ public void removePlayer(UUID uuid) {
plugin.getQuestsLogger().debug("Unloading and saving player " + uuid + ". Main thread: " + Bukkit.isPrimaryThread());
qPlayers.computeIfPresent(uuid, (mapUUID, qPlayer) -> {
- storageProvider.saveProgressFile(uuid, questProgressFile);
+ savePlayer(uuid);
return null;
});
}
/**
- * Saves the player to disk with a specified {@link QuestProgressFile}.
+ * Schedules a save for the player with the {@link QuestProgressFile} associated by the {@link QPlayerManager}.
+ * The modified status of the progress file will be reset.
*
* @param uuid the uuid of the player
- * @param questProgressFile the quest progress file to associate with and save
*/
- public void savePlayer(UUID uuid, QuestProgressFile questProgressFile) {
- plugin.getQuestsLogger().debug("Saving player " + uuid + ". Main thread: " + Bukkit.isPrimaryThread());
- storageProvider.saveProgressFile(uuid, questProgressFile);
+ public void savePlayer(UUID uuid) {
+ savePlayer(uuid, getPlayer(uuid).getQuestProgressFile());
}
/**
- * Saves the player to disk using the {@link QuestProgressFile} associated by the {@link QPlayerManager}
+ * Schedules a save for the player with a specified {@link QuestProgressFile}. The modified status of the
+ * specified progress file will be reset.
*
* @param uuid the uuid of the player
+ * @param originalProgressFile the quest progress file to associate with and save
*/
- public void savePlayer(UUID uuid) {
- savePlayer(uuid, getPlayer(uuid).getQuestProgressFile());
+ public void savePlayer(UUID uuid, QuestProgressFile originalProgressFile) {
+ QuestProgressFile clonedProgressFile = new QuestProgressFile(originalProgressFile);
+ originalProgressFile.resetModified();
+ plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> save(uuid, clonedProgressFile));
+ }
+
+ /**
+ * Immediately saves the player with the {@link QuestProgressFile} associated by the {@link QPlayerManager},
+ * on the same thread. The modified status of the specified progress file is not changed.
+ *
+ * @param uuid the uuid of the player
+ */
+ public void savePlayerSync(UUID uuid) {
+ savePlayerSync(uuid, getPlayer(uuid).getQuestProgressFile());
+ }
+
+ /**
+ * Immediately saves the player with a specified {@link QuestProgressFile}, on the same thread. The modified status
+ * of the specified progress file is not changed.
+ *
+ * @param uuid the uuid of the player
+ * @param questProgressFile the quest progress file to associate with and save
+ */
+ public void savePlayerSync(UUID uuid, QuestProgressFile questProgressFile) {
+ save(uuid, questProgressFile);
+ }
+
+ private void save(UUID uuid, QuestProgressFile questProgressFile) {
+ plugin.getQuestsLogger().debug("Saving player " + uuid + ". Main thread: " + Bukkit.isPrimaryThread());
+ storageProvider.saveProgressFile(uuid, questProgressFile);
}
/**
@@ -107,7 +135,7 @@ public class QPlayerManager {
}
/**
- * Load the player from disk if they exist, otherwise create a new {@link QuestProgressFile}.
+ * Load the player if they exist, otherwise create a new {@link QuestProgressFile}.
* This will have no effect if player is already loaded. Can be invoked asynchronously.
*
* @param uuid the uuid of the player
diff --git a/src/main/java/com/leonardobishop/quests/player/questprogressfile/QuestProgress.java b/src/main/java/com/leonardobishop/quests/player/questprogressfile/QuestProgress.java
index 7d795fed..d7cac084 100644
--- a/src/main/java/com/leonardobishop/quests/player/questprogressfile/QuestProgress.java
+++ b/src/main/java/com/leonardobishop/quests/player/questprogressfile/QuestProgress.java
@@ -120,8 +120,7 @@ public class QuestProgress {
this.addTaskProgress(taskProgress);
}
- @Deprecated // this shit is annoying to maintain
- public boolean isWorthSaving() {
+ public boolean isModified() {
if (modified) return true;
else {
for (TaskProgress progress : this.taskProgress.values()) {
@@ -138,7 +137,7 @@ public class QuestProgress {
public void resetModified() {
this.modified = false;
for (TaskProgress progress : this.taskProgress.values()) {
- progress.setModified(false);
+ progress.resetModified();
}
}
}
diff --git a/src/main/java/com/leonardobishop/quests/player/questprogressfile/QuestProgressFile.java b/src/main/java/com/leonardobishop/quests/player/questprogressfile/QuestProgressFile.java
index 0ddf3525..ccdb1db1 100644
--- a/src/main/java/com/leonardobishop/quests/player/questprogressfile/QuestProgressFile.java
+++ b/src/main/java/com/leonardobishop/quests/player/questprogressfile/QuestProgressFile.java
@@ -244,4 +244,10 @@ public class QuestProgressFile {
}
}
+ public void resetModified() {
+ for (QuestProgress questProgress : questProgress.values()) {
+ questProgress.resetModified();
+ }
+ }
+
}
diff --git a/src/main/java/com/leonardobishop/quests/player/questprogressfile/TaskProgress.java b/src/main/java/com/leonardobishop/quests/player/questprogressfile/TaskProgress.java
index 14e1bea0..e0943ff3 100644
--- a/src/main/java/com/leonardobishop/quests/player/questprogressfile/TaskProgress.java
+++ b/src/main/java/com/leonardobishop/quests/player/questprogressfile/TaskProgress.java
@@ -67,7 +67,7 @@ public class TaskProgress {
return modified;
}
- public void setModified(boolean modified) {
- this.modified = modified;
+ public void resetModified() {
+ this.modified = false;
}
}
diff --git a/src/main/java/com/leonardobishop/quests/storage/MySqlStorageProvider.java b/src/main/java/com/leonardobishop/quests/storage/MySqlStorageProvider.java
index 5789d8a0..bac0b5c9 100644
--- a/src/main/java/com/leonardobishop/quests/storage/MySqlStorageProvider.java
+++ b/src/main/java/com/leonardobishop/quests/storage/MySqlStorageProvider.java
@@ -7,7 +7,6 @@ import com.leonardobishop.quests.player.questprogressfile.TaskProgress;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.bukkit.configuration.ConfigurationSection;
-import org.bukkit.configuration.MemorySection;
import org.bukkit.configuration.file.YamlConfiguration;
import java.sql.Connection;
@@ -205,7 +204,7 @@ public class MySqlStorageProvider implements StorageProvider {
@Override
public void saveProgressFile(UUID uuid, QuestProgressFile questProgressFile) {
try (Connection connection = hikari.getConnection()) {
- plugin.getQuestsLogger().debug("Saving player " + uuid);
+ plugin.getQuestsLogger().debug("Getting known entries for player " + uuid);
List<String> knownQuestIds = new ArrayList<>();
Map<String, List<String>> knownTaskIds = new HashMap<>();
try (PreparedStatement ps = connection.prepareStatement(this.statementProcessor.apply(SELECT_KNOWN_PLAYER_QUEST_PROGRESS))) {
@@ -238,6 +237,8 @@ public class MySqlStorageProvider implements StorageProvider {
List<QuestProgress> questProgressValues = new ArrayList<>(questProgressFile.getAllQuestProgress());
for (QuestProgress questProgress : questProgressValues) {
+ if (!questProgress.isModified()) continue;
+
String questId = questProgress.getQuestId();
if (knownQuestIds.contains(questId)) {
updateQuestProgress.setBoolean(1, questProgress.isStarted());
@@ -301,13 +302,9 @@ public class MySqlStorageProvider implements StorageProvider {
}
}
- System.out.println(insertQuestProgress);
insertQuestProgress.executeBatch();
- System.out.println(insertTaskProgress);
insertTaskProgress.executeBatch();
- System.out.println(updateQuestProgress);
updateQuestProgress.executeBatch();
- System.out.println(updateTaskProgress);
updateTaskProgress.executeBatch();
}
} catch (SQLException e) {