From dd139c4e67a0c013a5b528199105d58ef0c179a8 Mon Sep 17 00:00:00 2001 From: Robin Date: Sat, 23 Nov 2024 06:56:55 +0000 Subject: [PATCH] Fix Data Loading --- .../oribuin/fishing/listener/PlayerListeners.java | 14 +++++++++++++- .../oribuin/fishing/manager/base/DataManager.java | 8 +++----- src/main/resources/plugin.yml | 2 +- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/main/java/xyz/oribuin/fishing/listener/PlayerListeners.java b/src/main/java/xyz/oribuin/fishing/listener/PlayerListeners.java index 87bdb37..a125511 100644 --- a/src/main/java/xyz/oribuin/fishing/listener/PlayerListeners.java +++ b/src/main/java/xyz/oribuin/fishing/listener/PlayerListeners.java @@ -9,6 +9,8 @@ import xyz.oribuin.fishing.manager.base.DataManager; import xyz.oribuin.fishing.storage.Fisher; +import java.util.concurrent.CompletableFuture; + public class PlayerListeners implements Listener { private final FishingPlugin plugin; @@ -24,7 +26,17 @@ public PlayerListeners(FishingPlugin plugin) { */ @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST) public void onJoin(PlayerJoinEvent event) { - this.plugin.getManager(DataManager.class).loadUser(event.getPlayer().getUniqueId()); + DataManager manager = this.plugin.getManager(DataManager.class); + + CompletableFuture.runAsync(() -> manager.loadUser(event.getPlayer().getUniqueId())) + .thenRun(() -> { + // create a new Fisher object if the player is not found + Fisher fisher = manager.get(event.getPlayer().getUniqueId()); + if (fisher == null) fisher = new Fisher(event.getPlayer().getUniqueId()); + + // Save the new user data + manager.saveUser(fisher); + }); } /** diff --git a/src/main/java/xyz/oribuin/fishing/manager/base/DataManager.java b/src/main/java/xyz/oribuin/fishing/manager/base/DataManager.java index d121a00..4dc02ad 100644 --- a/src/main/java/xyz/oribuin/fishing/manager/base/DataManager.java +++ b/src/main/java/xyz/oribuin/fishing/manager/base/DataManager.java @@ -49,10 +49,7 @@ public void reload() { * @return The user's data */ public Fisher get(UUID uuid) { - return this.userData.computeIfAbsent(uuid, x -> { - this.loadUser(uuid); - return null; - }); + return this.userData.get(uuid); } /** @@ -154,8 +151,9 @@ private void loadUser(UUID uuid, Connection connection) throws SQLException { fisher.experience(result.getInt("experience")); fisher.points(result.getInt("skill_points")); fisher.skills(GSON.fromJson(result.getString("skills"), PlayerSkills.class).skills()); - this.userData.put(uuid, fisher); } + + this.userData.put(uuid, fisher); } } diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 3c548c8..7186cb0 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,6 +1,6 @@ name: Fishing main: xyz.oribuin.fishing.FishingPlugin version: '@version@' -api-version: '1.20' +api-version: '1.21' author: Oribuin description: fishing :) \ No newline at end of file