diff --git a/README.md b/README.md index d766643..551ddea 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,32 @@ # Chest Networks -ChestNetworks is a Bukkit plugin which allows you to create networks of chests; chests that sort items between themselves. +ChestNetworks allows you to create networks of chests; chests that sort items between themselves. + +This is useful for automatic warehouses. + +## Usage + +There are two kinds of chests in a chest network: + +- **input** - don't store items, but only send them into storage chests if possible +- **storage** - store items + - Can have defined list of items to store, other items will be sent elsewhere if possible. + - If it has no list of items, it's a "misc" chest and will contain any items that don't fit in other chests + +_Storage_ chests will try to send "foreign" items to the correct chest, meaning you can just dump your whole inventory into any chest in the network and let the network handle it. + +Sorting is triggered when closing the chest's inventory and by hoppers (if the event isn't disabled\*). + +### ChestNetwork command: + +/%s - Main command (Aliases: %s) + +#### Subcommands: + +help - Displays this help (Alias: h) +create - Creates new chest network with the given name (Alias: c) +delete - Deletes the whole chest network (doesn't ask, double check the name before deleting) +ist - Shows a list of your chest networks (Aliases: listNets, listNetworks) +addChest [content...] - Adds new chest into the named chest network of given type or changes the setting for already registered chest; if the type is 'storage', you need to specify what items the chest should contain, you can put multiple names of items separated by spaces; If you don't specify any contents, the network will put items that can't go anywhere else into this chest (a 'misc.' chest). After entering this command, you will be asked to right-click the desired chest. (Alias: setChest) +checkChest - Shows info about the chest. After entering this command, you will be asked to right-click the desired chest. (Alias: check) +cancelSelect - Cancels selecting a chest and cancels previously typed ChestNetworks command if you're required to select a chest. (Aliases: cancel) diff --git a/TODO.md b/TODO.md index 26ed197..3d4b7ae 100644 --- a/TODO.md +++ b/TODO.md @@ -2,5 +2,9 @@ [X] Proper file names [ ] Use ACF? +[ ] ACF localization? [ ] Better separation of concerns [ ] Proper (de)serialization +[ ] YAML translation file +[ ] Proper help +[ ] Online manual diff --git a/src/main/java/eu/zhincore/chestnetworks/ChestNetController.java b/src/main/java/eu/zhincore/chestnetworks/ChestNetController.java index 1f7e69a..3883c9b 100644 --- a/src/main/java/eu/zhincore/chestnetworks/ChestNetController.java +++ b/src/main/java/eu/zhincore/chestnetworks/ChestNetController.java @@ -202,9 +202,10 @@ private void addChest(Player player, Location loc, String[] playerData) { String playerId = player.getUniqueId().toString(); JSONObject jsonLoc = (JSONObject) ChestNetDatabase.locToJson(loc); JSONArray network = (JSONArray) ((JSONObject) players.get(playerId)).get(playerData[1]); + Inventory inv = getChestByLoc(loc).getInventory(); // Detect is this chest is already registered - Chest registeredChest = getRegisteredChest(getChestByLoc(loc).getInventory()); + Chest registeredChest = getRegisteredChest(inv); if (registeredChest != null) { JSONObject chestData = getChestData(registeredChest.getLocation()); network.remove(chestData); @@ -220,6 +221,7 @@ private void addChest(Player player, Location loc, String[] playerData) { network.add(chest); database.save(); plugin.messenger.send("chadded", player, playerData[1]); + update(inv); } @SuppressWarnings("unchecked") @@ -291,7 +293,7 @@ public void update(Inventory inventory) { // Try to send all of its items to the storage for (ItemStack item : inventory.getStorageContents()) { if (item == null) continue; - changedChests.add(placeItems(network, inventory, item)); + changedChests.add(placeItems(network, chestData, item)); } // If it is a storage chest } else { @@ -299,7 +301,7 @@ public void update(Inventory inventory) { for (ItemStack item : inventory.getStorageContents()) { if (item == null) continue; if (!((JSONArray) chestData.get("content")).contains(item.getType().toString())) { - changedChests.add(placeItems(network, inventory, item)); + changedChests.add(placeItems(network, chestData, item)); } } // Trigger update for input chests in case new empty spot exists @@ -330,8 +332,8 @@ private void sortInventory(Inventory inventory) { } } - private JSONObject placeItems(JSONArray network, Inventory inventory, ItemStack item) { - if (item == null) return null; + private JSONObject placeItems(JSONArray network, JSONObject originChestData, ItemStack stack) { + if (stack == null) return null; Function findPlace = fallback -> { for (Object chest : network) { @@ -339,21 +341,24 @@ private JSONObject placeItems(JSONArray network, Inventory inventory, ItemStack JSONArray chestcontent = (JSONArray) chestconf.get("content"); if (!chestconf.get("type").equals("storage") - || !(chestcontent.isEmpty() ? fallback : chestcontent.contains(item.getType().toString()))) + || !(chestcontent.isEmpty() ? fallback : chestcontent.contains(stack.getType().toString()))) continue; + // Item is already in suitable destination + if (chestconf.equals(originChestData)) return chestconf; + Location loc = ChestNetDatabase.jsonToLoc((JSONObject) chestconf.get("location")); Chest target = getChestByLoc(loc); if (target == null) continue; Inventory destination = target.getInventory(); - if (destination.equals(inventory)) continue; - HashMap overflow = destination.addItem(item); - Integer remains = 0; + HashMap overflow = destination.addItem(stack.clone()); + + int remains = 0; if (!overflow.isEmpty()) { remains = overflow.get(0).getAmount(); } - item.setAmount(remains); + stack.setAmount(remains); return chestconf; } diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index fe367d5..44911d1 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -3,7 +3,7 @@ version: 1.0-SNAPSHOT description: ChestNetworks allows you to create networks of chests; chests that sort items between themselves. author: Zhincore api-version: 1.18 -main: eu.zhincore.chestnetworks.ChestNetworks +main: eu.zhincore.chestnetworks.ChestNetworksPlugin softdepend: [ChestSort]