Skip to content

Commit

Permalink
fix: concurrent add exception
Browse files Browse the repository at this point in the history
  • Loading branch information
NgLoader committed Dec 29, 2023
1 parent f32f8ff commit 3de14a3
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions zip-plugin/src/main/java/net/imprex/zip/BackpackHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

import org.bukkit.NamespacedKey;
import org.bukkit.inventory.Inventory;
Expand Down Expand Up @@ -36,6 +38,7 @@ public class BackpackHandler implements ZIPHandler {

private Map<UniqueId, Backpack> backpackById = new ConcurrentHashMap<>();
private Map<Inventory, Backpack> backpackByInventory = new ConcurrentHashMap<>();
private List<UniqueId> loadingIssue = new CopyOnWriteArrayList<>();

public BackpackHandler(BackpackPlugin plugin) {
this.plugin = plugin;
Expand Down Expand Up @@ -71,14 +74,22 @@ public Backpack loadBackpack(Path file) {

Backpack backpack = new Backpack(this.plugin, buffer);
return backpack;
} catch (IOException e) {
} catch (Exception e) {
ZIPLogger.error("Unable to load backpack for id '" + file.getFileName().toString() + "'", e);
}
return null;
}

public Backpack loadBackpack(UniqueId id) {
return this.loadBackpack(this.folderPath.resolve(id.toString()));
if (this.loadingIssue.contains(id)) {
return null;
}

Backpack backpack = this.loadBackpack(this.folderPath.resolve(id.toString()));
if (backpack == null) {
this.loadingIssue.add(id);
}
return backpack;
}

@Override
Expand Down Expand Up @@ -106,7 +117,11 @@ public void save(ZIPBackpack backpack) {

@Override
public Backpack getBackpack(ZIPUniqueId id) {
return this.backpackById.computeIfAbsent((UniqueId) id, __ -> this.loadBackpack((UniqueId) id));
Backpack backpack = this.backpackById.get(id);
if (backpack == null) {
backpack = this.loadBackpack((UniqueId) id);
}
return backpack;
}

@Override
Expand Down

0 comments on commit 3de14a3

Please sign in to comment.