From f32f8fffa134ef3cd3cf461dd681feda9732acfe Mon Sep 17 00:00:00 2001 From: Nils Gereke Date: Fri, 29 Dec 2023 01:26:39 +0100 Subject: [PATCH] fix: #11 backpacks now loading when required --- .../java/net/imprex/zip/BackpackHandler.java | 37 +++++-------------- .../java/net/imprex/zip/BackpackListener.java | 13 ++++--- .../java/net/imprex/zip/BackpackPlugin.java | 1 - .../net/imprex/zip/config/MessageKey.java | 3 +- 4 files changed, 18 insertions(+), 36 deletions(-) diff --git a/zip-plugin/src/main/java/net/imprex/zip/BackpackHandler.java b/zip-plugin/src/main/java/net/imprex/zip/BackpackHandler.java index 11df6d5..6c19449 100644 --- a/zip-plugin/src/main/java/net/imprex/zip/BackpackHandler.java +++ b/zip-plugin/src/main/java/net/imprex/zip/BackpackHandler.java @@ -3,7 +3,6 @@ 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.Map; @@ -24,6 +23,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 { @@ -35,7 +35,6 @@ public class BackpackHandler implements ZIPHandler { private final Path folderPath; private Map backpackById = new ConcurrentHashMap<>(); - private Map backpackByItem = new ConcurrentHashMap<>(); private Map backpackByInventory = new ConcurrentHashMap<>(); public BackpackHandler(BackpackPlugin plugin) { @@ -57,18 +56,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) { @@ -81,15 +70,17 @@ 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(); + 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())); + } + @Override public void save(ZIPBackpack backpack) { if (Files.notExists(this.folderPath)) { @@ -115,7 +106,7 @@ public void save(ZIPBackpack backpack) { @Override public Backpack getBackpack(ZIPUniqueId id) { - return this.backpackById.get(id); + return this.backpackById.computeIfAbsent((UniqueId) id, __ -> this.loadBackpack((UniqueId) id)); } @Override @@ -129,11 +120,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(); @@ -164,11 +150,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); diff --git a/zip-plugin/src/main/java/net/imprex/zip/BackpackListener.java b/zip-plugin/src/main/java/net/imprex/zip/BackpackListener.java index 276ff37..7a2b896 100644 --- a/zip-plugin/src/main/java/net/imprex/zip/BackpackListener.java +++ b/zip-plugin/src/main/java/net/imprex/zip/BackpackListener.java @@ -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 @@ -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); } } } @@ -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); } } } \ No newline at end of file diff --git a/zip-plugin/src/main/java/net/imprex/zip/BackpackPlugin.java b/zip-plugin/src/main/java/net/imprex/zip/BackpackPlugin.java index 6861ce4..2d4d5a2 100644 --- a/zip-plugin/src/main/java/net/imprex/zip/BackpackPlugin.java +++ b/zip-plugin/src/main/java/net/imprex/zip/BackpackPlugin.java @@ -49,7 +49,6 @@ public void onEnable() { this.backpackConfig.deserialize(); this.backpackRegistry.register(); - this.backpackHandler.loadBackpacks(); this.updateSystem = new UpdateSystem(this); diff --git a/zip-plugin/src/main/java/net/imprex/zip/config/MessageKey.java b/zip-plugin/src/main/java/net/imprex/zip/config/MessageKey.java index c9b2c94..50e99e5 100644 --- a/zip-plugin/src/main/java/net/imprex/zip/config/MessageKey.java +++ b/zip-plugin/src/main/java/net/imprex/zip/config/MessageKey.java @@ -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()) {