Skip to content

Commit

Permalink
Fixed duping bug and started writing README
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhincore committed Jun 9, 2022
1 parent 76757eb commit abb3c1c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 12 deletions.
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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 <subcommand> - Main command (Aliases: %s)

#### Subcommands:

help - Displays this help (Alias: h)
create <new_network_name> - Creates new chest network with the given name (Alias: c)
delete <network_name> - 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 <network_name> <chest_type ('storage' or 'input')> [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)
4 changes: 4 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
25 changes: 15 additions & 10 deletions src/main/java/eu/zhincore/chestnetworks/ChestNetController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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")
Expand Down Expand Up @@ -291,15 +293,15 @@ 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 {
// Find items that do not belong there
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
Expand Down Expand Up @@ -330,30 +332,33 @@ 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<Boolean, JSONObject> findPlace = fallback -> {
for (Object chest : network) {
JSONObject chestconf = (JSONObject) chest;
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<Integer, ItemStack> overflow = destination.addItem(item);
Integer remains = 0;
HashMap<Integer, ItemStack> overflow = destination.addItem(stack.clone());

int remains = 0;
if (!overflow.isEmpty()) {
remains = overflow.get(0).getAmount();
}
item.setAmount(remains);
stack.setAmount(remains);

return chestconf;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down

0 comments on commit abb3c1c

Please sign in to comment.