Skip to content

Commit

Permalink
Fix: #11 backpacks now loading when required (#16)
Browse files Browse the repository at this point in the history
* fix: #11 backpacks now loading when required

* fix: concurrent add exception
  • Loading branch information
NgLoader authored Dec 29, 2023
1 parent 3edb078 commit 43fa3bc
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 37 deletions.
54 changes: 25 additions & 29 deletions zip-plugin/src/main/java/net/imprex/zip/BackpackHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.FileVisitOption;
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 All @@ -24,6 +25,7 @@
import net.imprex.zip.api.ZIPUniqueId;
import net.imprex.zip.common.Ingrim4Buffer;
import net.imprex.zip.common.UniqueId;
import net.imprex.zip.util.ZIPLogger;

public class BackpackHandler implements ZIPHandler {

Expand All @@ -35,8 +37,8 @@ public class BackpackHandler implements ZIPHandler {
private final Path folderPath;

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

public BackpackHandler(BackpackPlugin plugin) {
this.plugin = plugin;
Expand All @@ -57,18 +59,8 @@ void registerBackpack(Backpack backpack) {

public void disable() {
this.backpackById.values().forEach(Backpack::save);
}

public void loadBackpacks() {
try {
if (Files.notExists(this.folderPath)) {
Files.createDirectories(this.folderPath);
}

Files.walk(this.folderPath, FileVisitOption.FOLLOW_LINKS).forEach(this::loadBackpack);
} catch (IOException e) {
e.printStackTrace();
}
this.backpackById.clear();
this.backpackByInventory.clear();
}

public Backpack loadBackpack(Path file) {
Expand All @@ -81,15 +73,25 @@ public Backpack loadBackpack(Path file) {
Ingrim4Buffer buffer = new Ingrim4Buffer(Unpooled.wrappedBuffer(data));

Backpack backpack = new Backpack(this.plugin, buffer);
this.backpackById.put(backpack.getId(), backpack);

return backpack;
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
ZIPLogger.error("Unable to load backpack for id '" + file.getFileName().toString() + "'", e);
}
return null;
}

public Backpack loadBackpack(UniqueId id) {
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
public void save(ZIPBackpack backpack) {
if (Files.notExists(this.folderPath)) {
Expand All @@ -115,7 +117,11 @@ public void save(ZIPBackpack backpack) {

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

@Override
Expand All @@ -129,11 +135,6 @@ public Backpack getBackpack(ItemStack item) {
return null;
}

Backpack backpack = this.backpackByItem.get(item);
if (backpack != null) {
return backpack;
}

if (item != null && item.hasItemMeta()) {
ItemMeta meta = item.getItemMeta();
PersistentDataContainer dataContainer = meta.getPersistentDataContainer();
Expand Down Expand Up @@ -164,11 +165,6 @@ public boolean isBackpack(ItemStack item) {
return false;
}

Backpack backpack = this.backpackByItem.get(item);
if (backpack != null) {
return true;
}

if (item != null && item.hasItemMeta()) {
ItemMeta meta = item.getItemMeta();
return meta.getPersistentDataContainer().has(this.backpackIdentifierKey, PersistentDataType.STRING);
Expand Down
13 changes: 7 additions & 6 deletions zip-plugin/src/main/java/net/imprex/zip/BackpackListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,21 @@

import net.imprex.zip.api.ZIPBackpackType;
import net.imprex.zip.api.ZIPRecipe;
import net.imprex.zip.config.MessageConfig;
import net.imprex.zip.config.MessageKey;

public class BackpackListener implements Listener {

private final BackpackHandler backpackHandler;
private final BackpackRegistry backpackRegistry;
private final UpdateSystem updateSystem;
private final MessageConfig messageConfig;

public BackpackListener(BackpackPlugin plugin) {
this.backpackHandler = plugin.getBackpackHandler();
this.backpackRegistry = plugin.getBackpackRegistry();
this.updateSystem = plugin.getUpdateSystem();
this.messageConfig = plugin.getBackpackConfig().message();
}

@EventHandler
Expand Down Expand Up @@ -102,6 +106,8 @@ public void onPlayerInteract(PlayerInteractEvent event) {
Backpack backpack = this.backpackHandler.getBackpack(event.getItem());
if (backpack != null) {
backpack.open(event.getPlayer());
} else {
this.messageConfig.send(event.getPlayer(), MessageKey.UnableToLoadBackpack);
}
}
}
Expand All @@ -117,12 +123,7 @@ public void onBlockPlace(BlockPlaceEvent event) {
public void onCraftItem(CraftItemEvent event) {
ItemStack item = event.getCurrentItem();
if (this.backpackHandler.isBackpack(item)) {
if (event.isShiftClick()) {
event.setCancelled(true);
return;
}

this.backpackHandler.getBackpack(item);
event.setCancelled(true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public void onEnable() {
this.backpackConfig.deserialize();

this.backpackRegistry.register();
this.backpackHandler.loadBackpacks();

this.updateSystem = new UpdateSystem(this);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ public enum MessageKey {
LoreLineCreate("loreLineCreate", "The lore line {0} was added"),
LoreLineChange("loreLineChange", "The lore line {0} was changed"),
LoreLineDelete("loreLineDelete", "The lore line {0} was deleted"),
MaxLoreCountReached("maxLoreCountReached", "You have reached the max lore count of {0}");
MaxLoreCountReached("maxLoreCountReached", "You have reached the max lore count of {0}"),
UnableToLoadBackpack("unableToLoadBackpack", "Backpack can't be loaded!");

public static MessageKey findByKey(String key) {
for (MessageKey messageKey : values()) {
Expand Down

0 comments on commit 43fa3bc

Please sign in to comment.