From ec6698602c9033b339ed34f056909227ec4acc19 Mon Sep 17 00:00:00 2001 From: DNx Date: Sat, 11 Apr 2020 05:32:18 +0700 Subject: [PATCH] optimize Cargo Node's filter index saving. --- .../core/networks/cargo/CargoUtils.java | 47 ++++++++++--------- .../Slimefun/api/BlockStorage.java | 6 +-- 2 files changed, 27 insertions(+), 26 deletions(-) diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/cargo/CargoUtils.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/cargo/CargoUtils.java index f9404e164..cb145638c 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/cargo/CargoUtils.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/cargo/CargoUtils.java @@ -61,15 +61,15 @@ final class CargoUtils { ItemStack[] contents = inv.getContents(); int minSlot = 0; int maxSlot = contents.length; - + if (inv instanceof FurnaceInventory) { minSlot = 2; maxSlot = 3; - } + } else if (inv instanceof BrewerInventory) { maxSlot = 3; } - + for (int slot = minSlot; slot < maxSlot; slot++) { ItemStack itemInSlot = contents[slot]; // Mapped into inventory @@ -77,14 +77,14 @@ final class CargoUtils { if (itemInSlot.getAmount() > template.getAmount()) { itemInSlot.setAmount(itemInSlot.getAmount() - template.getAmount()); return template; - } + } else { itemInSlot.setAmount(itemInSlot.getAmount() - template.getAmount()); return itemInSlot; } } } - + return null; } @@ -184,21 +184,21 @@ final class CargoUtils { if (stack.getType().isFuel()) { minSlot = 1; maxSlot = 2; - } + } else { maxSlot = 1; } - } + } else if (inv instanceof BrewerInventory) { //Check if it goes in the potion slot, if (stack.getType() == Material.POTION || stack.getType() == Material.LINGERING_POTION || stack.getType() == Material.SPLASH_POTION) { maxSlot = 3; //The blaze powder slot, - } + } else if (stack.getType() == Material.BLAZE_POWDER) { minSlot = 4; maxSlot = 5; - } + } else { //Or the input minSlot = 3; @@ -212,7 +212,7 @@ final class CargoUtils { if (itemInSlot == null) { inv.setItem(slot, stack); return null; - } + } else { int maxStackSize = itemInSlot.getType().getMaxStackSize(); if (SlimefunUtils.isItemSimilar(itemInSlot, stack, true, false) && itemInSlot.getAmount() < maxStackSize) { @@ -231,7 +231,7 @@ final class CargoUtils { } } } - + return stack; } @@ -256,37 +256,38 @@ final class CargoUtils { boolean lore = "true".equals(blockInfo.getString("filter-lore")); if ("whitelist".equals(blockInfo.getString("filter-type"))) { - List items = new ArrayList<>(); + List templateItems = new ArrayList<>(); for (int slot : SLOTS) { ItemStack template = menu.getItemInSlot(slot); if (template != null) { - items.add(template); + templateItems.add(template); } } - if (items.isEmpty()) { + if (templateItems.isEmpty()) { return false; } if (index >= 0) { index++; - if (index > (items.size() - 1)) index = 0; + if (index > (templateItems.size() - 1)) index = 0; - BlockStorage.addBlockInfo(block, "index", String.valueOf(index)); + blockInfo.setValue("index", String.valueOf(index)); + BlockStorage.setBlockInfo(block, blockInfo, false); - return SlimefunUtils.isItemSimilar(item, items.get(index), lore); - } + return SlimefunUtils.isItemSimilar(item, templateItems.get(index), lore); + } else { - for (ItemStack stack : items) { + for (ItemStack stack : templateItems) { if (SlimefunUtils.isItemSimilar(item, stack, lore)) { - return true; + return true; } } - + return false; } - } + } else { for (int slot : SLOTS) { ItemStack itemInSlot = menu.getItemInSlot(slot); @@ -294,7 +295,7 @@ final class CargoUtils { return false; } } - + return true; } } diff --git a/src/main/java/me/mrCookieSlime/Slimefun/api/BlockStorage.java b/src/main/java/me/mrCookieSlime/Slimefun/api/BlockStorage.java index 8d632379f..4327b21a6 100644 --- a/src/main/java/me/mrCookieSlime/Slimefun/api/BlockStorage.java +++ b/src/main/java/me/mrCookieSlime/Slimefun/api/BlockStorage.java @@ -43,6 +43,7 @@ public class BlockStorage { private static final String PATH_BLOCKS = "data-storage/Slimefun/stored-blocks/"; private static final String PATH_CHUNKS = "data-storage/Slimefun/stored-chunks/"; + private static final String PATH_INVENTORIES = "data-storage/Slimefun/stored-inventories/"; private final World world; private final Map storage = new ConcurrentHashMap<>(); @@ -450,14 +451,13 @@ public class BlockStorage { } } else if (!storage.hasInventory(l)) { - File file = new File("data-storage/Slimefun/stored-inventories/" + serializeLocation(l) + ".sfi"); - + File file = new File(PATH_INVENTORIES + serializeLocation(l) + ".sfi"); if (file.exists()) storage.inventories.put(l, new BlockMenu(BlockMenuPreset.getPreset(id), l, new io.github.thebusybiscuit.cscorelib2.config.Config(file))); else storage.loadInventory(l, BlockMenuPreset.getPreset(id)); } } - refreshCache(getStorage(l.getWorld()), l, id, serializeBlockInfo(cfg), updateTicker); + refreshCache(storage, l, id, serializeBlockInfo(cfg), updateTicker); } public static void setBlockInfo(Block b, String json, boolean updateTicker) {