Skip to content

Commit

Permalink
Fix Data Loading
Browse files Browse the repository at this point in the history
  • Loading branch information
Oribuin committed Nov 23, 2024
1 parent 2152cca commit dd139c4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
14 changes: 13 additions & 1 deletion src/main/java/xyz/oribuin/fishing/listener/PlayerListeners.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -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 :)

0 comments on commit dd139c4

Please sign in to comment.