Skip to content

Commit

Permalink
fix: #11 backpacks now loading when required
Browse files Browse the repository at this point in the history
  • Loading branch information
NgLoader committed Dec 29, 2023
1 parent 3edb078 commit f32f8ff
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 36 deletions.
37 changes: 9 additions & 28 deletions zip-plugin/src/main/java/net/imprex/zip/BackpackHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand All @@ -35,7 +35,6 @@ 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<>();

public BackpackHandler(BackpackPlugin plugin) {
Expand All @@ -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) {
Expand All @@ -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)) {
Expand All @@ -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
Expand All @@ -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();
Expand Down Expand Up @@ -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);
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 f32f8ff

Please sign in to comment.