diff --git a/.gitignore b/.gitignore index dc61bff..adc8407 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ .gradle /build/ +/bin/ /target/ # Ignore Gradle GUI config diff --git a/build.gradle b/build.gradle index 6aa0147..8f8f110 100644 --- a/build.gradle +++ b/build.gradle @@ -1,42 +1,40 @@ -buildscript { - repositories { - jcenter() - } - - dependencies { - classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.2' - } -} - - plugins { + id 'com.github.johnrengelman.shadow' version '7.1.2' id 'java' } apply plugin: 'com.github.johnrengelman.shadow' repositories { + mavenCentral() maven { url = uri('https://papermc.io/repo/repository/maven-public/') } - maven { url = uri('https://repo.aikar.co/content/groups/aikar/') } + maven { + url = uri('https://hub.jeff-media.com/nexus/repository/jeff-media-public/') + } } dependencies { - runtimeOnly 'io.papermc.paper:paper-api:1.18.2-R0.1-SNAPSHOT' + compileOnly 'io.papermc.paper:paper-api:1.18.2-R0.1-SNAPSHOT' + compileOnly 'de.jeff_media:ChestSortAPI:13.0.0-SNAPSHOT' implementation 'co.aikar:taskchain-bukkit:3.7.2' } -group = 'eu.zhincore.chestNetworks' +group = 'eu.zhincore.chestnetworks' version = '1.0-SNAPSHOT' -description = 'chest-networks' -java.sourceCompatibility = JavaVersion.VERSION_16 +description = 'chestnetworks' shadowJar { - relocate 'co.aikar.taskchain', 'eu.zhincore.chestNetworks.taskchain' + relocate 'co.aikar.taskchain', 'eu.zhincore.chestnetworks.taskchain' + archiveClassifier = "shadowed" +} + +java { + toolchain.languageVersion.set(JavaLanguageVersion.of(17)) } build.dependsOn shadowJar diff --git a/settings.gradle b/settings.gradle index a4e96fb..2d99d05 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,5 +1 @@ -/* - * This file was generated by the Gradle 'init' task. - */ - -rootProject.name = 'chest-networks' +rootProject.name = 'ChestNetworks' diff --git a/src/main/java/eu/zhincore/chestnetworks/ChestNetworks.java b/src/main/java/eu/zhincore/chestnetworks/ChestNetworks.java index ac8b499..4e0fb0b 100644 --- a/src/main/java/eu/zhincore/chestnetworks/ChestNetworks.java +++ b/src/main/java/eu/zhincore/chestnetworks/ChestNetworks.java @@ -1,7 +1,6 @@ package eu.zhincore.chestnetworks; import java.io.IOException; - import org.bukkit.plugin.java.JavaPlugin; import org.json.simple.parser.ParseException; diff --git a/src/main/java/eu/zhincore/chestnetworks/CommandChestNet.java b/src/main/java/eu/zhincore/chestnetworks/CommandChestNet.java index 2e56951..5e6caeb 100644 --- a/src/main/java/eu/zhincore/chestnetworks/CommandChestNet.java +++ b/src/main/java/eu/zhincore/chestnetworks/CommandChestNet.java @@ -1,7 +1,6 @@ package eu.zhincore.chestnetworks; import java.util.Arrays; - import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -24,8 +23,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St return true; } - if (args.length < 1) - return false; + if (args.length < 1) return false; // Sanitize arguments for (int i = args.length - 1; i != 0; i--) { @@ -36,8 +34,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St switch (args[0]) { case "h": case "help": - plugin.messenger.send("helpmsg", (Player) sender, - command.getName(), String.join(", ", command.getAliases())); + plugin.messenger.send("helpmsg", (Player) sender, command.getName(), String.join(", ", command.getAliases())); break; case "c": @@ -49,7 +46,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St networksController.create((Player) sender, args[1]); break; - case "delete": + case "deleteNetwork": if (args.length < 2) { sender.sendMessage(ChatColor.GOLD + "Use: " + ChatColor.RED + "/" + label + " delete "); return true; @@ -59,8 +56,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St case "setChest": case "addChest": - if (args.length < 3 - || !(args[2].equals("storage") || args[2].equals("input"))) { + if (args.length < 3 || !(args[2].equals("storage") || args[2].equals("input"))) { sender.sendMessage(ChatColor.GOLD + "Use: " + ChatColor.RED + "/" + label + " addChest [contents...]"); return true; @@ -79,6 +75,17 @@ public boolean onCommand(CommandSender sender, Command command, String label, St // TODO break; + case "sorting": + if (args.length < 3) { + sender.sendMessage( + ChatColor.GOLD + "Use: " + ChatColor.RED + "/" + label + " sorting "); + return true; + } + var enabled = args[2].equalsIgnoreCase("on"); + networksController.setSorting(((Player) sender).getUniqueId().toString(), args[1], enabled); + sender.sendMessage("Sorting changed"); + break; + case "check": case "checkChest": networksController.startCheckChest((Player) sender); diff --git a/src/main/java/eu/zhincore/chestnetworks/CommandChestNetTabCompleter.java b/src/main/java/eu/zhincore/chestnetworks/CommandChestNetTabCompleter.java index 1bd16a1..753d8d4 100644 --- a/src/main/java/eu/zhincore/chestnetworks/CommandChestNetTabCompleter.java +++ b/src/main/java/eu/zhincore/chestnetworks/CommandChestNetTabCompleter.java @@ -3,7 +3,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; - import org.bukkit.Material; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -22,19 +21,20 @@ public List onTabComplete(CommandSender sender, Command command, String if (networksController.isPlayerSelectQueue((Player) sender)) { return Arrays.asList(new String[] { "cancelSelect" }); } else if (args.length == 1) { - return Arrays.asList(new String[] { - "help", "create", "delete", - "addChest", "setChest", - "list", "checkChest" - }); + return Arrays.asList( + new String[] { "help", "create", "deleteNetwork", "addChest", "setChest", "list", "checkChest", "sorting" }); } else { switch (args[0]) { case "create": - case "delete": + case "deleteNetwork": if (args.length == 2) { return Arrays.asList(new String[] { "" }); } return new ArrayList<>(); + case "sorting": + if (args.length == 2) return getNets((Player) sender); + if (args.length == 3) return Arrays.asList(new String[] { "on", "off" }); + return new ArrayList<>(); case "setChest": case "addChest": if (args.length == 2) { diff --git a/src/main/java/eu/zhincore/chestnetworks/Database.java b/src/main/java/eu/zhincore/chestnetworks/Database.java index 4cc1fac..17fcb96 100644 --- a/src/main/java/eu/zhincore/chestnetworks/Database.java +++ b/src/main/java/eu/zhincore/chestnetworks/Database.java @@ -5,7 +5,6 @@ import java.io.FileWriter; import java.io.InputStreamReader; import java.io.PrintWriter; - import org.bukkit.Bukkit; import org.bukkit.Location; import org.json.simple.JSONObject; diff --git a/src/main/java/eu/zhincore/chestnetworks/EventListener.java b/src/main/java/eu/zhincore/chestnetworks/EventListener.java index cd88b4b..7ee8849 100644 --- a/src/main/java/eu/zhincore/chestnetworks/EventListener.java +++ b/src/main/java/eu/zhincore/chestnetworks/EventListener.java @@ -46,7 +46,7 @@ public void onBlockBreakEvent(BlockBreakEvent ev) { } @EventHandler - public void onInventoryCloseEvent​(InventoryCloseEvent ev) { + public void onInventoryCloseEvent(InventoryCloseEvent ev) { onInventoryUpdate(ev.getInventory()); } diff --git a/src/main/java/eu/zhincore/chestnetworks/Messenger.java b/src/main/java/eu/zhincore/chestnetworks/Messenger.java index f8057be..9bf025e 100644 --- a/src/main/java/eu/zhincore/chestnetworks/Messenger.java +++ b/src/main/java/eu/zhincore/chestnetworks/Messenger.java @@ -4,7 +4,6 @@ import java.io.Reader; import java.util.ArrayList; import java.util.List; - import org.bukkit.command.CommandSender; import org.json.simple.JSONArray; import org.json.simple.JSONObject; diff --git a/src/main/java/eu/zhincore/chestnetworks/NetworksController.java b/src/main/java/eu/zhincore/chestnetworks/NetworksController.java index bd0fb8b..c9d76c6 100644 --- a/src/main/java/eu/zhincore/chestnetworks/NetworksController.java +++ b/src/main/java/eu/zhincore/chestnetworks/NetworksController.java @@ -6,7 +6,7 @@ import java.util.List; import java.util.function.Function; import java.util.stream.Collectors; - +import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; @@ -33,14 +33,14 @@ public NetworksController(ChestNetworks plugin) { } public Boolean isPlayerSelectQueue(Player player) { - String playerID = player.getUniqueId().toString(); - return playerSelectQueue.containsKey(playerID); + String playerId = player.getUniqueId().toString(); + return playerSelectQueue.containsKey(playerId); } public void cancelPlayerSelect(Player player) { - String playerID = player.getUniqueId().toString(); - if (playerSelectQueue.containsKey(playerID)) { - playerSelectQueue.remove(playerID); + String playerId = player.getUniqueId().toString(); + if (playerSelectQueue.containsKey(playerId)) { + playerSelectQueue.remove(playerId); plugin.messenger.send("selrmvd", player); return; } @@ -57,33 +57,33 @@ public void list(Player player) { } public List listNets(Player player) { - String playerID = player.getUniqueId().toString(); - return listNets(playerID); + String playerId = player.getUniqueId().toString(); + return listNets(playerId); } @SuppressWarnings("unchecked") - public List listNets(String playerID) { - if (players.containsKey(playerID)) { - return new ArrayList(((JSONObject) players.get(playerID)).keySet()); + public List listNets(String playerId) { + if (players.containsKey(playerId)) { + return new ArrayList(((JSONObject) players.get(playerId)).keySet()); } return null; } public List listChests(Player player, String name) { - String playerID = player.getUniqueId().toString(); - return listChests(playerID, name); + String playerId = player.getUniqueId().toString(); + return listChests(playerId, name); } @SuppressWarnings("unchecked") - public List listChests(String playerID, String netName) { - if (players.containsKey(playerID)) { - JSONObject network = (JSONObject) players.get(playerID); + public List listChests(String playerId, String netName) { + if (players.containsKey(playerId)) { + JSONObject network = (JSONObject) players.get(playerId); if (network.containsKey(netName)) { var chests = new ArrayList((JSONArray) network.get(netName)); // Add info to each chest, used when referencing the network for (var chest : chests) { - chest.put("owner", playerID); + chest.put("owner", playerId); chest.put("network", netName); } return chests; @@ -94,11 +94,11 @@ public List listChests(String playerID, String netName) { @SuppressWarnings("unchecked") public List listGlobalChests() { - List playerIDs = new ArrayList<>(players.keySet()); + List playerIds = new ArrayList<>(players.keySet()); List output = new ArrayList<>(); - for (String playerID : playerIDs) { - for (String networkName : listNets(playerID)) { - output.addAll(listChests(playerID, networkName)); + for (String playerId : playerIds) { + for (String networkName : listNets(playerId)) { + output.addAll(listChests(playerId, networkName)); } } return output; @@ -106,22 +106,21 @@ public List listGlobalChests() { @SuppressWarnings("unchecked") public List listGlobalChestLocations() { - List playerIDs = new ArrayList<>(players.keySet()); + List playerIds = new ArrayList<>(players.keySet()); List output = new ArrayList<>(); - for (String playerID : playerIDs) { - for (String networkName : listNets(playerID)) { - output.addAll(listChests(playerID, networkName) - .stream().map(v -> ((Location) Database.jsonToLoc((JSONObject) v.get("location")))) - .collect(Collectors.toList())); + for (String playerId : playerIds) { + for (String networkName : listNets(playerId)) { + output.addAll(listChests(playerId, networkName).stream() + .map(v -> ((Location) Database.jsonToLoc((JSONObject) v.get("location")))).collect(Collectors.toList())); } } return output; } public void delete(Player player, String name) { - String playerID = player.getUniqueId().toString(); - if (players.containsKey(playerID)) { - JSONObject networks = (JSONObject) players.get(playerID); + String playerId = player.getUniqueId().toString(); + if (players.containsKey(playerId)) { + JSONObject networks = (JSONObject) players.get(playerId); if (networks.containsKey(name)) { networks.remove(name); plugin.messenger.send("cnetdel", player, name); @@ -134,11 +133,11 @@ public void delete(Player player, String name) { @SuppressWarnings("unchecked") public void create(Player player, String name) { - String playerID = player.getUniqueId().toString(); - if (!players.containsKey(playerID)) { - players.put(playerID, new JSONObject()); + String playerId = player.getUniqueId().toString(); + if (!players.containsKey(playerId)) { + players.put(playerId, new JSONObject()); } - JSONObject networks = (JSONObject) players.get(playerID); + JSONObject networks = (JSONObject) players.get(playerId); if (networks.containsKey(name)) { plugin.messenger.send("cnet409", player, name); return; @@ -154,18 +153,18 @@ public void create(Player player, String name) { } public void startAddChest(Player player, String name, String type, List content) { - String playerID = player.getUniqueId().toString(); - if (!players.containsKey(playerID)) { + String playerId = player.getUniqueId().toString(); + if (!players.containsKey(playerId)) { plugin.messenger.send("cnet404", player, name); return; } - JSONObject networks = (JSONObject) players.get(playerID); + JSONObject networks = (JSONObject) players.get(playerId); if (!networks.containsKey(name)) { plugin.messenger.send("cnet404", player, name); return; } - if (playerSelectQueue.containsKey(playerID)) { + if (playerSelectQueue.containsKey(playerId)) { // Already selecting a chest plugin.messenger.send("cnet425", player); return; @@ -179,16 +178,15 @@ public void startCheckChest(Player player) { } private void startPlayerSelect(Player player, String[] data) { - String playerID = player.getUniqueId().toString(); - playerSelectQueue.put(playerID, data); + String playerId = player.getUniqueId().toString(); + playerSelectQueue.put(playerId, data); plugin.messenger.send("selectc", player); } public void onPlayerSelected(Player player, Location loc) { - String playerID = player.getUniqueId().toString(); - if (!playerSelectQueue.containsKey(playerID)) - return; - String[] playerData = playerSelectQueue.remove(playerID); + String playerId = player.getUniqueId().toString(); + if (!playerSelectQueue.containsKey(playerId)) return; + String[] playerData = playerSelectQueue.remove(playerId); switch (playerData[0]) { case "add": addChest(player, loc, playerData); @@ -201,9 +199,9 @@ public void onPlayerSelected(Player player, Location loc) { @SuppressWarnings("unchecked") private void addChest(Player player, Location loc, String[] playerData) { - String playerID = player.getUniqueId().toString(); + String playerId = player.getUniqueId().toString(); JSONObject jsonLoc = (JSONObject) Database.locToJson(loc); - JSONArray network = (JSONArray) ((JSONObject) players.get(playerID)).get(playerData[1]); + JSONArray network = (JSONArray) ((JSONObject) players.get(playerId)).get(playerData[1]); // Detect is this chest is already registered Chest registeredChest = getRegisteredChest(getChestByLoc(loc).getInventory()); @@ -234,12 +232,8 @@ private void checkChest(Player player, Location loc) { JSONObject chestData = getChestData(registeredChest.getLocation()); String type = (String) chestData.get("type"); JSONArray content = (JSONArray) chestData.get("content"); - plugin.messenger.send("chcheck", player, - (String) chestData.get("network"), - (String) chestData.get("owner"), - type, - type.equals("input") ? "N/A" - : content.size() == 0 ? "§oEverything§r" : (String) String.join(", ", content)); + plugin.messenger.send("chcheck", player, (String) chestData.get("network"), (String) chestData.get("owner"), type, + type.equals("input") ? "N/A" : content.size() == 0 ? "§oEverything§r" : (String) String.join(", ", content)); } public JSONObject getChestData(Location loc) { @@ -253,8 +247,7 @@ public JSONObject getChestData(Location loc) { public void removeChest(Location loc, Player player) { JSONObject chest = getChestData(loc); - if (chest == null) - return; + if (chest == null) return; ((JSONArray) ((JSONObject) players.get(chest.get("owner"))).get(chest.get("network"))).remove(chest); if (player != null) { plugin.messenger.send("chesrem", (CommandSender) player, (String) chest.get("network")); @@ -262,29 +255,51 @@ public void removeChest(Location loc, Player player) { database.save(); } + public void setSorting(String playerId, String netName, boolean enabled) { + JSONArray network = (JSONArray) ((JSONObject) players.get(playerId)).get(netName); + for (Object chestDatai : network) { + JSONObject chestData = (JSONObject) chestDatai; + if (enabled) chestData.put("sort", true); + else chestData.remove("sort"); + } + database.save(); + + update(playerId, netName); + } + + public void update(String playerId, String netName) { + JSONArray network = (JSONArray) ((JSONObject) players.get(playerId)).get(netName); + + for (Object chestData : network) { + Chest chest = getChestByLoc(Database.jsonToLoc((JSONObject) ((JSONObject) chestData).get("location"))); + if (chest != null) { + update(chest.getInventory()); + } + } + } + public void update(Inventory inventory) { Chest chest = getRegisteredChest(inventory); - if (chest == null) - return; + if (chest == null) return; JSONObject chestData = getChestData(chest.getLocation()); JSONArray network = (JSONArray) ((JSONObject) players.get(chestData.get("owner"))).get(chestData.get("network")); + var changedChests = new ArrayList(); + // If it is an input chest if (((String) chestData.get("type")).equals("input")) { // Try to send all of its items to the storage - for (ItemStack item : inventory.getContents()) { - if (item == null) - continue; - placeItems(network, inventory, item); + for (ItemStack item : inventory.getStorageContents()) { + if (item == null) continue; + changedChests.add(placeItems(network, inventory, item)); } // If it is a storage chest } else { // Find items that do not belong there - for (ItemStack item : inventory.getContents()) { - if (item == null) - continue; + for (ItemStack item : inventory.getStorageContents()) { + if (item == null) continue; if (!((JSONArray) chestData.get("content")).contains(item.getType().toString())) { - placeItems(network, inventory, item); + changedChests.add(placeItems(network, inventory, item)); } } // Trigger update for input chests in case new empty spot exists @@ -298,50 +313,64 @@ public void update(Inventory inventory) { } } } + + if (chestData.get("sort") != null) sortInventory(inventory); + for (var _chestData : changedChests) { + if (_chestData == null || _chestData.get("sort") != null) continue; + Chest _chest = getChestByLoc(Database.jsonToLoc((JSONObject) ((JSONObject) _chestData).get("location"))); + sortInventory(_chest.getInventory()); + } } } - private void placeItems(JSONArray network, Inventory source, ItemStack item) { - if (item == null) - return; + private void sortInventory(Inventory inventory) { + if (Bukkit.getPluginManager().getPlugin("ChestSort") != null) { + de.jeff_media.chestsort.api.ChestSortAPI.sortInventory(inventory); + } + } + + private JSONObject placeItems(JSONArray network, Inventory inventory, ItemStack item) { + if (item == null) return null; - Function findPlace = fallback -> { + Function 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())) { + if (!chestconf.get("type").equals("storage") + || !(chestcontent.isEmpty() ? fallback : chestcontent.contains(item.getType().toString()))) + continue; - Location loc = Database.jsonToLoc((JSONObject) chestconf.get("location")); - Chest target = getChestByLoc(loc); - if (target == null) - continue; + Location loc = Database.jsonToLoc((JSONObject) chestconf.get("location")); + Chest target = getChestByLoc(loc); + if (target == null) continue; - Inventory destination = target.getInventory(); - HashMap overflow = destination.addItem(item); - Integer remains = 0; - if (!overflow.isEmpty()) { - remains = overflow.get(0).getAmount(); - } - item.setAmount(remains); - return true; + Inventory destination = target.getInventory(); + if (destination.equals(inventory)) continue; + HashMap overflow = destination.addItem(item); + Integer remains = 0; + if (!overflow.isEmpty()) { + remains = overflow.get(0).getAmount(); } + item.setAmount(remains); + + return chestconf; } - return false; + return null; }; - if (!findPlace.apply(false)) { + var chestconf = findPlace.apply(false); + if (chestconf == null) { // Try to put the item into misc chest - findPlace.apply(true); + chestconf = findPlace.apply(true); } + return chestconf; } private Chest getRegisteredChest(Inventory inventory) { Chest[] chests = getChests(inventory); - if (chests == null) - return null; + if (chests == null) return null; List registeredChestLocs = listGlobalChestLocations(); for (Chest chest : chests) { diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index f34b9d2..fe367d5 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,9 +1,11 @@ -name: chest-networks +name: ChestNetworks 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.ChestNetworks + +softdepend: [ChestSort] commands: chestnet: