From 139bc5019b47327fddb1d3300d788d147da1da91 Mon Sep 17 00:00:00 2001 From: Kwantux Date: Fri, 11 Oct 2024 15:54:58 +0200 Subject: [PATCH 01/16] Renamed all occurrences of NanoFlux to Kwantux --- LICENSE | 2 +- README.md | 4 ++-- src/main/resources/lang/de.yml | 2 +- src/main/resources/lang/en.yml | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/LICENSE b/LICENSE index d064577..6411f24 100644 --- a/LICENSE +++ b/LICENSE @@ -5,7 +5,7 @@ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - Copyright (c) 2022 - 2024 NanoFlux + Copyright (c) 2022 - 2024 Kwantux 1. Definitions. diff --git a/README.md b/README.md index 121ba3b..3e13df9 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ This Project is developed by @Quantum625 [Modrinth](https://modrinth.com/plugin/networks) -[Hangar](https://hangar.papermc.io/NanoFlux/Networks) +[Hangar](https://hangar.papermc.io/Kwantux/Networks) [Bukkit](https://dev.bukkit.org/projects/networks) @@ -53,7 +53,7 @@ To remove a chest from your network, simply just break it ## More information Most commands and their functionalities can be seen in /networks help -See a more detailed instruction at the [wiki](https://github.com/nanoflux/networks/wiki) +See a more detailed instruction at the [wiki](https://github.com/Kwantux/networks/wiki) ## Installation diff --git a/src/main/resources/lang/de.yml b/src/main/resources/lang/de.yml index d265ad0..cfbda91 100755 --- a/src/main/resources/lang/de.yml +++ b/src/main/resources/lang/de.yml @@ -1,6 +1,6 @@ prefix: "[Networks] " -version: "Networks Plugin von NanoFlux\nVersion: <1>" +version: "Networks Plugin von Kwantux\nVersion: <1>" help: "\n Networks v<1> - Help Menu diff --git a/src/main/resources/lang/en.yml b/src/main/resources/lang/en.yml index 6ae3034..a7ac1a6 100755 --- a/src/main/resources/lang/en.yml +++ b/src/main/resources/lang/en.yml @@ -1,6 +1,6 @@ prefix: "[Networks] " -version: "Networks Plugin by NanoFlux\nVersion: <1>" +version: "Networks Plugin by Kwantux\nVersion: <1>" help: "\n Networks v<1> - Help Menu From 6b8a061d4186f40101770aac30da951e96f98473 Mon Sep 17 00:00:00 2001 From: Kwantux Date: Sun, 10 Nov 2024 13:16:52 +0100 Subject: [PATCH 02/16] Strict item filters --- src/main/java/de/kwantux/networks/Main.java | 9 ++ .../java/de/kwantux/networks/Manager.java | 7 ++ .../networks/commands/NetworksCommand.java | 17 +++- .../networks/compat/LegacyNetwork.java | 7 +- .../networks/component/NetworkComponent.java | 6 +- .../component/component/SortingContainer.java | 63 ++++++++---- .../component/util/FilterTranslator.java | 96 +++++++++++++++++++ .../kwantux/networks/event/WandListener.java | 37 +++++-- .../networks/storage/ComponentSerializer.java | 22 +++++ src/main/resources/lang/de.yml | 9 ++ src/main/resources/lang/en.yml | 10 ++ 11 files changed, 254 insertions(+), 29 deletions(-) create mode 100644 src/main/java/de/kwantux/networks/component/util/FilterTranslator.java diff --git a/src/main/java/de/kwantux/networks/Main.java b/src/main/java/de/kwantux/networks/Main.java index 7338fb6..3b6f279 100755 --- a/src/main/java/de/kwantux/networks/Main.java +++ b/src/main/java/de/kwantux/networks/Main.java @@ -1,6 +1,7 @@ package de.kwantux.networks; import de.kwantux.networks.commands.NetworksCommandManager; +import de.kwantux.networks.component.util.FilterTranslator; import de.kwantux.networks.utils.DoubleChestUtils; import de.kwantux.networks.utils.FoliaUtils; import de.kwantux.networks.utils.Metrics; @@ -90,12 +91,20 @@ public void onEnable() { lang = new LanguageController(this, cfg.getLanguage(), "en", "de"); + + new NetworksCommandManager(this); regionScheduler = getServer().getRegionScheduler(); globalRegionScheduler = getServer().getGlobalRegionScheduler(); asyncScheduler = getServer().getAsyncScheduler(); + try { + FilterTranslator.load(Path.of(getDataFolder().getAbsolutePath(), "filters.txt")); + } catch (IOException e) { + throw new RuntimeException(e); + } + mgr = new Manager(this); crf = new CraftingManager(this); diff --git a/src/main/java/de/kwantux/networks/Manager.java b/src/main/java/de/kwantux/networks/Manager.java index db886c5..f67d344 100755 --- a/src/main/java/de/kwantux/networks/Manager.java +++ b/src/main/java/de/kwantux/networks/Manager.java @@ -1,6 +1,7 @@ package de.kwantux.networks; +import de.kwantux.networks.component.util.FilterTranslator; import de.kwantux.networks.utils.DoubleChestUtils; import de.kwantux.networks.component.ComponentType; import de.kwantux.networks.component.NetworkComponent; @@ -12,6 +13,7 @@ import org.bukkit.persistence.PersistentDataContainer; import javax.annotation.Nullable; +import java.io.IOException; import java.util.*; import java.util.logging.Logger; @@ -103,6 +105,11 @@ public void saveData() { storage.saveNetwork(id, networks.get(id)); } logger.info("Saved " + networks.size() + " Networks"); + try { + FilterTranslator.save(); + } catch (IOException e) { + throw new RuntimeException(e); + } } diff --git a/src/main/java/de/kwantux/networks/commands/NetworksCommand.java b/src/main/java/de/kwantux/networks/commands/NetworksCommand.java index 993ba08..0a1a404 100755 --- a/src/main/java/de/kwantux/networks/commands/NetworksCommand.java +++ b/src/main/java/de/kwantux/networks/commands/NetworksCommand.java @@ -7,6 +7,7 @@ import de.kwantux.networks.component.component.InputContainer; import de.kwantux.networks.component.component.MiscContainer; import de.kwantux.networks.component.component.SortingContainer; +import de.kwantux.networks.component.util.FilterTranslator; import de.kwantux.networks.config.Config; import de.kwantux.networks.inventory.InventoryMenuManager; import de.kwantux.networks.utils.BlockLocation; @@ -33,7 +34,9 @@ import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.Set; import java.util.UUID; +import java.util.stream.Collectors; import static de.kwantux.networks.Main.*; import static de.kwantux.networks.commands.ComponentTypeParser.componentTypeParser; @@ -503,8 +506,18 @@ public static Component componentInfo(Network network, @Nullable NetworkComponen return switch (component) { case InputContainer container -> lang.get("wand.info.input", network.name(), component.pos().toString(), String.valueOf(container.range())); - case SortingContainer container -> - lang.get("wand.info.sorting", network.name(), component.pos().toString(), String.valueOf(container.acceptorPriority()), Arrays.stream(container.filters()).toList().toString()); + case SortingContainer container -> { + Component filters = Component.text("["); + Set filterSet = Arrays.stream(container.filters()).mapToObj( + FilterTranslator::translate + ).collect(Collectors.toSet()); + for (Component filter : filterSet) { + filters = filters.append(filter); + filters = filters.append(Component.text(", ")); + } + filters = filters.append(Component.text("]")); + yield lang.get("wand.info.sorting", network.displayText(), component.pos().displayText(), Component.text(String.valueOf(container.acceptorPriority())), filters); + } case MiscContainer container -> lang.get("wand.info.misc", network.name(), component.pos().toString(), String.valueOf(container.acceptorPriority())); case null, default -> Component.empty(); diff --git a/src/main/java/de/kwantux/networks/compat/LegacyNetwork.java b/src/main/java/de/kwantux/networks/compat/LegacyNetwork.java index 009e675..424cdf6 100644 --- a/src/main/java/de/kwantux/networks/compat/LegacyNetwork.java +++ b/src/main/java/de/kwantux/networks/compat/LegacyNetwork.java @@ -10,6 +10,7 @@ import de.kwantux.networks.component.component.MiscContainer; import de.kwantux.networks.utils.BlockLocation; import org.bukkit.Bukkit; +import org.bukkit.Material; import org.jetbrains.annotations.Nullable; import java.io.IOException; @@ -90,7 +91,11 @@ record LegacySortingContainer( ) { public @Nullable SortingContainer convert() { if (pos == null) return null; - return new SortingContainer(pos.convert(), items, priority); + int[] filters = new int[items.length]; + for (int i = 0; i < items.length; i++) { + filters[i] = Objects.requireNonNullElse(Material.getMaterial(items[i]), Material.AIR).ordinal(); + } + return new SortingContainer(pos.convert(), SortingContainer.convertLegacyFilters(items), priority); } } diff --git a/src/main/java/de/kwantux/networks/component/NetworkComponent.java b/src/main/java/de/kwantux/networks/component/NetworkComponent.java index 76616ea..951e40f 100755 --- a/src/main/java/de/kwantux/networks/component/NetworkComponent.java +++ b/src/main/java/de/kwantux/networks/component/NetworkComponent.java @@ -118,13 +118,13 @@ else if (value instanceof Byte) { else if (value instanceof Boolean) { container.set(NamespaceUtils.key(key), PersistentDataType.BYTE, (boolean) value ? (byte) 1 : (byte) 0); } - else if (value instanceof Integer[]) { + else if (value instanceof int[]) { container.set(NamespaceUtils.key(key), PersistentDataType.INTEGER_ARRAY, (int[]) value); } - else if (value instanceof Long[]) { + else if (value instanceof long[]) { container.set(NamespaceUtils.key(key), PersistentDataType.LONG_ARRAY, (long[]) value); } - else if (value instanceof Byte[]) { + else if (value instanceof byte[]) { container.set(NamespaceUtils.key(key), PersistentDataType.BYTE_ARRAY, (byte[]) value); } else { diff --git a/src/main/java/de/kwantux/networks/component/component/SortingContainer.java b/src/main/java/de/kwantux/networks/component/component/SortingContainer.java index d6a602c..387bc4a 100755 --- a/src/main/java/de/kwantux/networks/component/component/SortingContainer.java +++ b/src/main/java/de/kwantux/networks/component/component/SortingContainer.java @@ -8,6 +8,7 @@ import de.kwantux.networks.utils.BlockLocation; import net.kyori.adventure.text.Component; import org.apache.commons.lang3.ArrayUtils; +import org.bukkit.Material; import org.bukkit.inventory.ItemStack; import org.bukkit.persistence.PersistentDataContainer; import org.bukkit.persistence.PersistentDataType; @@ -15,8 +16,10 @@ import javax.annotation.Nonnull; import java.util.Arrays; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.stream.IntStream; public class SortingContainer extends NetworkComponent implements Acceptor, Supplier { @@ -25,26 +28,42 @@ public ComponentType type() { return type; } - private String[] filters; - private int acceptorPriority = 10; - private int supplierPriority = 0; + private int[] filters; + private int acceptorPriority; + private int supplierPriority; public static SortingContainer create(BlockLocation pos, PersistentDataContainer container) { - if (container == null) return new SortingContainer(pos, new String[0], 10); + if (container == null) return new SortingContainer(pos, new int[0], 10, 0); + + int[] filters; + + try { + filters = container.get(NamespaceUtils.FILTERS.key(), PersistentDataType.INTEGER_ARRAY); // Normal deserialization + } catch (Exception e) { + filters = convertLegacyFilters( // If deserialization fails, try legacy deserialization + Objects.requireNonNullElse( + Objects.requireNonNullElse( // Try legacy deserialization + container.get(NamespaceUtils.FILTERS.key(), PersistentDataType.STRING), + null).split(","), + new String[0]) // If legacy deserialization fails, use empty array + ); + } + return new SortingContainer(pos, - Objects.requireNonNullElse(container.get(NamespaceUtils.FILTERS.key(), PersistentDataType.STRING).split(","), new String[0]), + filters, Objects.requireNonNullElse(container.get(NamespaceUtils.ACCEPTOR_PRIORITY.key(), PersistentDataType.INTEGER), 10), Objects.requireNonNullElse(container.get(NamespaceUtils.SUPPLIER_PRIORITY.key(), PersistentDataType.INTEGER), 0) ); } - public SortingContainer(BlockLocation pos, String[] filters, int acceptorPriority) { + public SortingContainer(BlockLocation pos, int[] filters, int acceptorPriority) { super(pos); this.filters = filters; this.acceptorPriority = acceptorPriority; + this.supplierPriority = 0; } - public SortingContainer(BlockLocation pos, String[] filters, int acceptorPriority, int supplierPriority) { + public SortingContainer(BlockLocation pos, int[] filters, int acceptorPriority, int supplierPriority) { super(pos); this.filters = filters; this.acceptorPriority = acceptorPriority; @@ -77,14 +96,18 @@ public static ComponentType register() { @Override public boolean fillMissingData() { if (filters == null) - filters = new String[0]; - + filters = new int[0]; return pos != null; } @Override public boolean accept(@Nonnull ItemStack stack) { - return Arrays.stream(filters).anyMatch(stack.getType().toString()::equalsIgnoreCase); + int matHash = stack.getItemMeta().hashCode(); + int metaHash = stack.getItemMeta().hashCode(); + for (int filter : filters) { + if (matHash == filter || metaHash == filter) return true; + } + return false; } public int acceptorPriority() { @@ -111,7 +134,7 @@ public int supplierPriority() { return supplierPriority; } - public String[] filters() { + public int[] filters() { return filters; } @@ -121,16 +144,24 @@ public Map properties() { return new HashMap<>() {{ put(NamespaceUtils.ACCEPTOR_PRIORITY.name, acceptorPriority); put(NamespaceUtils.SUPPLIER_PRIORITY.name, supplierPriority); - put(NamespaceUtils.FILTERS.name, String.join(",", filters)); + put(NamespaceUtils.FILTERS.name, filters); }}; } - public void addFilter(String material) { + public void addFilter(int filter) { filters = Arrays.copyOf(filters, filters.length + 1); - filters[filters.length - 1] = material; + filters[filters.length - 1] = filter; + } + + public void removeFilter(int filter) { + filters = ArrayUtils.removeElement(filters, filter); } - public void removeFilter(String material) { - filters = ArrayUtils.removeElement(filters, material); + public static int[] convertLegacyFilters(String[] filters) { + int[] newFilters = new int[filters.length]; + for (int i = 0; i < filters.length; i++) { + newFilters[i] = Objects.requireNonNullElse(Material.getMaterial(filters[i]), Material.AIR).ordinal(); + } + return newFilters; } } diff --git a/src/main/java/de/kwantux/networks/component/util/FilterTranslator.java b/src/main/java/de/kwantux/networks/component/util/FilterTranslator.java new file mode 100644 index 0000000..a7387d1 --- /dev/null +++ b/src/main/java/de/kwantux/networks/component/util/FilterTranslator.java @@ -0,0 +1,96 @@ +package de.kwantux.networks.component.util; + +import de.kwantux.networks.Main; +import net.kyori.adventure.key.Key; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.event.HoverEvent; +import net.kyori.adventure.text.minimessage.MiniMessage; +import org.bukkit.Bukkit; +import org.bukkit.Material; + +import javax.annotation.Nullable; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.NoSuchFileException; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.stream.Collectors; +import java.util.stream.Stream; + + +/** + * Item Filters are stored as hashes of item meta.

+ * This translations list is used to get the item name from just the hash when a player wants to inspect the filters of a Sorting Container.
+ * (Otherwise it would just show a bunch of random numbers)

+ * Whenever a new item filter is added, it has to be added here.

+ * This class is purely QoL, essential functions should not depend on this. + */ +public class FilterTranslator { + + private static Path path = null; + + private static Map translations = new HashMap<>(); + + + public static Component translate(int id) { + return Objects.requireNonNullElse(translations.get(id), Component.text("#" + id)); + } + + private static void generateMaterialTranslations() throws IOException { + Main.logger.info("Generating translations mappings for item filters..."); + for (int i = 1; i < Material.values().length; i++) { + String translationKey = Material.values()[i].getItemTranslationKey(); + if (translationKey == null) continue; + translations.put(i, Component.translatable(translationKey) + .hoverEvent(HoverEvent.showItem( + HoverEvent.ShowItem.showItem( + Key.key(Material.values()[i].name().toLowerCase()), 1 + ) + )) + ); + } + save(); + Main.logger.info("Done generating translations mappings for item filters."); + } + + public static void updateTranslation(int id, Component translation) { + translations.put(id, translation); + } + + public static void save() throws IOException { + if (path == null) return; + List lines = new ArrayList<>(); + + lines.add("v\t" + Bukkit.getMinecraftVersion()); + lines.addAll(translations.entrySet().stream().map(e -> + e.getKey() + "\t" + MiniMessage.miniMessage().serialize(e.getValue()) + ).collect(Collectors.toSet())); + + Files.write(path, lines); + } + + public static void load(Path filePath) throws IOException { + path = filePath; + String minecraftVersion = null; + try { + for (String line : Files.readAllLines(filePath)) { + String[] split = line.split("\t"); + if (split[0].equals("v")) { + minecraftVersion = split[1]; + continue; + } + int id = Integer.parseInt(split[0]); + Component translation = MiniMessage.miniMessage().deserialize(split[1]); + translations.put(id, translation); + } + } catch (NoSuchFileException ignored) { // If there is no file, we have nothing to load + } + if (!Objects.equals(minecraftVersion, Bukkit.getMinecraftVersion())) + generateMaterialTranslations(); + } + +} diff --git a/src/main/java/de/kwantux/networks/event/WandListener.java b/src/main/java/de/kwantux/networks/event/WandListener.java index 9e56303..3f8b1af 100644 --- a/src/main/java/de/kwantux/networks/event/WandListener.java +++ b/src/main/java/de/kwantux/networks/event/WandListener.java @@ -11,12 +11,15 @@ import de.kwantux.networks.component.module.Acceptor; import de.kwantux.networks.component.module.Donator; import de.kwantux.networks.component.module.Requestor; +import de.kwantux.networks.component.util.FilterTranslator; import de.kwantux.networks.config.Config; import de.kwantux.networks.config.CraftingManager; import de.kwantux.networks.utils.BlockLocation; import de.kwantux.networks.utils.DoubleChestUtils; import de.kwantux.config.lang.LanguageController; import de.kwantux.networks.component.component.MiscContainer; +import net.kyori.adventure.key.Key; +import net.kyori.adventure.text.event.HoverEvent; import org.bukkit.Material; import org.bukkit.NamespacedKey; import org.bukkit.entity.Player; @@ -70,7 +73,7 @@ public void onPlayerInteract(PlayerInteractEvent event) throws InvalidNodeExcept if (action.equals(Action.LEFT_CLICK_BLOCK) || action.equals(Action.LEFT_CLICK_AIR)) { if (!p.isSneaking()) { mode++; - if (mode > 1) mode = 0; + if (mode > 2) mode = 0; //p.getInventory().setItemInMainHand(crf.getNetworkWand(mode)); event.getItem().setItemMeta(crafting.getNetworkWand(mode).getItemMeta()); lang.message(p, "wand.mode", lang.getRaw("wand.mode." + mode)); @@ -86,6 +89,7 @@ public void onPlayerInteract(PlayerInteractEvent event) throws InvalidNodeExcept if (!p.isSneaking()) { if (action.equals(Action.RIGHT_CLICK_BLOCK)) { p.sendMessage(NetworksCommand.componentInfo(network, component)); + if(component instanceof SortingContainer container) p.sendMessage(Arrays.toString(container.filters())); } return; } @@ -104,13 +108,15 @@ public void onPlayerInteract(PlayerInteractEvent event) throws InvalidNodeExcept return; } + ItemStack itemInOffHand = p.getInventory().getItemInOffHand(); if (action.equals(Action.RIGHT_CLICK_BLOCK)) { - if (mode == 0 && !p.getInventory().getItemInOffHand().getType().equals(Material.AIR) && net.getComponent(l) instanceof SortingContainer) { + if (mode == 0 && !itemInOffHand.getType().equals(Material.AIR) && net.getComponent(l) instanceof SortingContainer) { NetworkComponent c = net.getComponent(l); if (c instanceof SortingContainer container) { - container.addFilter(p.getInventory().getItemInOffHand().getType().toString().toUpperCase()); - lang.message(p, "component.sorting.setitem", l.toString(), p.getInventory().getItemInOffHand().getType().toString()); + container.addFilter(itemInOffHand.getType().ordinal()); + p.sendMessage(Arrays.toString(container.filters())); + lang.message(p, "component.sorting.setitem", l.toString(), itemInOffHand.getType().toString()); } } if (mode == 1) { @@ -119,15 +125,32 @@ public void onPlayerInteract(PlayerInteractEvent event) throws InvalidNodeExcept lang.message(p, "component.priority", String.valueOf(container.acceptorPriority())); } } + if (mode == 2 && !itemInOffHand.getType().equals(Material.AIR) && net.getComponent(l) instanceof SortingContainer) { + NetworkComponent c = net.getComponent(l); + if (c instanceof SortingContainer container) { + int hash = itemInOffHand.getItemMeta().hashCode(); + container.addFilter(hash); + FilterTranslator.updateTranslation(hash, itemInOffHand.displayName().hoverEvent(HoverEvent.showItem( + HoverEvent.ShowItem.showItem( + Key.key(itemInOffHand.getType().name().toLowerCase()), 1 + ) + ))); + p.sendMessage(Arrays.toString(container.filters())); + lang.message(p, "component.sorting.setitem", l.displayText(), itemInOffHand.displayName()); + } + } } if (action.equals(Action.LEFT_CLICK_BLOCK)) { - if (mode == 0 && net.getComponent(l) instanceof SortingContainer && !p.getInventory().getItemInOffHand().getType().equals(Material.AIR) && p.isSneaking()) { + if ((mode == 0 || mode == 2) && net.getComponent(l) instanceof SortingContainer && !itemInOffHand.getType().equals(Material.AIR) && p.isSneaking()) { NetworkComponent c = net.getComponent(l); if (c instanceof SortingContainer container) { - container.removeFilter(p.getInventory().getItemInOffHand().getType().toString().toUpperCase()); - lang.message(p, "component.sorting.removeitem", l.toString(), p.getInventory().getItemInOffHand().getType().toString()); + int hash = itemInOffHand.getItemMeta().hashCode(); + container.removeFilter(itemInOffHand.getType().ordinal()); + container.removeFilter(hash); + p.sendMessage(Arrays.toString(container.filters())); + lang.message(p, "component.sorting.removeitem", l.displayText(), itemInOffHand.displayName()); } } diff --git a/src/main/java/de/kwantux/networks/storage/ComponentSerializer.java b/src/main/java/de/kwantux/networks/storage/ComponentSerializer.java index bf1dec1..b070335 100644 --- a/src/main/java/de/kwantux/networks/storage/ComponentSerializer.java +++ b/src/main/java/de/kwantux/networks/storage/ComponentSerializer.java @@ -3,9 +3,12 @@ import com.google.gson.*; import de.kwantux.networks.component.NetworkComponent; import de.kwantux.networks.component.ComponentType; +import de.kwantux.networks.component.component.SortingContainer; +import de.kwantux.networks.component.util.FilterTranslator; import org.jetbrains.annotations.Nullable; import java.lang.reflect.Type; +import java.util.Arrays; import static de.kwantux.networks.Main.logger; @@ -18,6 +21,25 @@ public class ComponentSerializer implements JsonDeserializer, JsonObject jsonObject = json.getAsJsonObject(); ComponentType type = ComponentType.get(jsonObject.get("type").getAsString()); if (type == null) throw new JsonParseException("Error while deserializing network component. Unknown type: " + jsonObject.get("type").getAsString()); + + // Compat with old versions + if (type.equals(ComponentType.SORTING)) { + if (!jsonObject.get("filters").getAsJsonArray().isEmpty()) { + try { + jsonObject.get("filters").getAsJsonArray().get(0).getAsInt(); + } catch (NumberFormatException e) { + jsonObject.add("filters", gson.toJsonTree( + SortingContainer.convertLegacyFilters( + jsonObject.get("filters").getAsJsonArray().asList().stream().map( + (jsonPrimitive) -> jsonPrimitive.getAsString() + ).toArray(String[]::new) + ) + )); + } + + } + } + Class clazz = type.clazz; NetworkComponent component = gson.fromJson(json, clazz); if (!component.fillMissingData()) { diff --git a/src/main/resources/lang/de.yml b/src/main/resources/lang/de.yml index cfbda91..badd156 100755 --- a/src/main/resources/lang/de.yml +++ b/src/main/resources/lang/de.yml @@ -152,6 +152,7 @@ rename.taken: Name <1> ist bereits in Verwendung. wand.mode: "Konfigurator Modus wurde zu [<1>] geändert" wand.mode.0: "Filter" wand.mode.1: "Priorität" +wand.mode.2: "Filter (Strikt)" wand.info.input: "Typ: Einfüllkiste\nNetzwerk: <1>\nPosition: <2>\nExtra Reichweite: <3>\n" wand.info.sorting: "Typ: Sortierkiste\nNetzwerk: <1>\nPosition: <2>\nPriorität: <3>\nFilter: <4>\n" wand.info.misc: "Typ: Restekiste\nNetzwerk: <1>\nPosition: <2>\nPriorität: <3>\n" @@ -161,6 +162,7 @@ items.message: "Alle Items im Netzwerk <1>:\n( item.name.wand0: Netzwerk Konfigurator [Filter] item.name.wand1: Netzwerk Konfigurator [Priorität] +item.name.wand2: Netzwerk Konfigurator [Filter (Strikt)] item.name.component.input: Einfüllkiste item.name.component.sorting: Sortierkiste item.name.component.misc: Restekiste @@ -186,6 +188,13 @@ item.lore.wand1: - "" - "Links klicken, um den Modus zu wechseln" +item.lore.wand2: + - "Rechts klicke einen Netzwerkkomponenten, um Informationen über diesen zu sehen" + - "Schleichen + Rechts klicken, um das in deiner Zweithand liegende Item als Filter hinzuzufügen" + - "Schleichen + Links klicken, um das in deiner Zweithand liegende Item als Filter zu entfernen" + - "" + - "Links klicken, um den Modus zu wechseln" + item.lore.component.input: - "Sortiert eingelegte Items in Sortier- und Restekisten ein" item.lore.component.sorting: diff --git a/src/main/resources/lang/en.yml b/src/main/resources/lang/en.yml index a7ac1a6..47e2bf5 100755 --- a/src/main/resources/lang/en.yml +++ b/src/main/resources/lang/en.yml @@ -154,6 +154,7 @@ rename.taken: Name <1> is already taken by another network. wand.mode: "Wand mode set to [<1>]" wand.mode.0: "Filter" wand.mode.1: "Priority" +wand.mode.2: "Strict Filter" wand.info.input: "Type: Input Container\nNetwork: <1>\nLocation: <2>\nExtra Range: <3>\n" wand.info.sorting: "Type: Sorting Container\nNetwork: <1>\nLocation: <2>\nPriority: <3>\nFilters: <4>\n" wand.info.misc: "Type: Misc Container\nNetwork: <1>\nLocation: <2>\nPriority: <3>\n" @@ -163,6 +164,7 @@ items.message: "Total items in network <1>:\n( item.name.wand0: Network Wand [Filter] item.name.wand1: Network Wand [Priority] +item.name.wand2: Network Wand [Strict Filter] item.name.component.input: Input Container item.name.component.sorting: Sorting Container item.name.component.misc: Miscellaneous Container @@ -189,6 +191,14 @@ item.lore.wand1: - "" - "Left click to switch mode" + +item.lore.wand2: + - "Right click any network component to show information about it" + - "Sneak + Right click on a sorting chest with an item in the offhand to apply a filter" + - "Sneak + Left click with an item in the offhand to remove a filter" + - "" + - "Left click to switch mode" + item.lore.component.input: - "Sorts items into sorting containers and misc containers" item.lore.component.sorting: From 8fae8ab260225444835d406c62e1f94b52555805 Mon Sep 17 00:00:00 2001 From: Kwantux Date: Sun, 10 Nov 2024 13:31:54 +0100 Subject: [PATCH 03/16] Forbids placement of multiple containers inside each other --- .../networks/event/BlockPlaceListener.java | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/main/java/de/kwantux/networks/event/BlockPlaceListener.java b/src/main/java/de/kwantux/networks/event/BlockPlaceListener.java index b329d81..1cd8314 100644 --- a/src/main/java/de/kwantux/networks/event/BlockPlaceListener.java +++ b/src/main/java/de/kwantux/networks/event/BlockPlaceListener.java @@ -20,6 +20,8 @@ import org.bukkit.persistence.PersistentDataContainer; import org.bukkit.persistence.PersistentDataType; +import static de.kwantux.networks.Main.mgr; + public class BlockPlaceListener implements Listener { private final Manager net; @@ -61,7 +63,7 @@ public void onBlockPlace(BlockPlaceEvent event) { return; } - Main.mgr.createComponent(network, event.getBlock().getType(), type, pos, container); + mgr.createComponent(network, event.getBlock().getType(), type, pos, container); lang.message(p, "component."+type.tag+".add", network.name(), pos.toString()); } } @@ -80,14 +82,20 @@ public void onComponentInstall(PlayerInteractEvent event) { ItemStack item = event.getItem(); if (item == null) return; - if (item.getType().isBlock()) return; // This is handeled by the function above + if (item.getType().isBlock()) return; PersistentDataContainer container = item.getItemMeta().getPersistentDataContainer(); ComponentType type = ComponentType.get(container.get(NamespaceUtils.COMPONENT.key(), PersistentDataType.STRING)); - if (type == null) return; - if (config.checkLocation(pos, type)) { + event.setCancelled(true); // At this point, we know that the player is holding a component, so we suppress the default action (chest opens) + + if (mgr.getComponent(pos) != null) { // If there is a component already at the location, we don't install a new one + lang.message(p, "location.occupied"); + return; + } + + if (config.checkLocation(pos, type)) { // Check if the block can actually be a component (e.g. grass blocks cannot be input containers, but chests and barrels can) if (network == null) { lang.message(p, "select.noselection"); @@ -95,7 +103,7 @@ public void onComponentInstall(PlayerInteractEvent event) { return; } - Main.mgr.createComponent(network, event.getClickedBlock().getType(), type, pos, container); + mgr.createComponent(network, event.getClickedBlock().getType(), type, pos, container); item.setAmount(item.getAmount() - 1); lang.message(p, "component."+type.tag+".add", network.name(), pos.toString()); } From 689bade82ba93675c4a7bf428edeca61420a5426 Mon Sep 17 00:00:00 2001 From: Kwantux Date: Sun, 10 Nov 2024 13:32:10 +0100 Subject: [PATCH 04/16] Updated a comment --- .../java/de/kwantux/networks/component/NetworkComponent.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/de/kwantux/networks/component/NetworkComponent.java b/src/main/java/de/kwantux/networks/component/NetworkComponent.java index 951e40f..24b52d0 100755 --- a/src/main/java/de/kwantux/networks/component/NetworkComponent.java +++ b/src/main/java/de/kwantux/networks/component/NetworkComponent.java @@ -88,7 +88,7 @@ public static ItemStack item(ComponentType type, Map properties) * Copies all properties from the map to the persistent data container * @param container The persistent data container to edit * @param map The map of properties - * @throws IllegalArgumentException Supported data types are String, Integer, Long, Double, Float, Short, Byte, Boolean, Integer[], Long[], Byte[] + * @throws IllegalArgumentException Supported data types are String, Integer, Long, Double, Float, Short, Byte, Boolean, int[], long[], byte[] */ private static void mapToContainer(PersistentDataContainer container, Map map) { for (Map.Entry entry : map.entrySet()) { From 87ab3702ede82dd27b8fa77845321d91280c7ec2 Mon Sep 17 00:00:00 2001 From: Kwantux Date: Sun, 10 Nov 2024 13:32:36 +0100 Subject: [PATCH 05/16] Fixed the default properties of the Sorting Container --- .../kwantux/networks/component/component/SortingContainer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/de/kwantux/networks/component/component/SortingContainer.java b/src/main/java/de/kwantux/networks/component/component/SortingContainer.java index 387bc4a..cdd4ff4 100755 --- a/src/main/java/de/kwantux/networks/component/component/SortingContainer.java +++ b/src/main/java/de/kwantux/networks/component/component/SortingContainer.java @@ -73,7 +73,7 @@ public SortingContainer(BlockLocation pos, int[] filters, int acceptorPriority, private static Map defaultProperties = new HashMap<>(); static { - defaultProperties.put(NamespaceUtils.FILTERS.name, ""); + defaultProperties.put(NamespaceUtils.FILTERS.name, new int[0]); defaultProperties.put(NamespaceUtils.ACCEPTOR_PRIORITY.name, 10); defaultProperties.put(NamespaceUtils.SUPPLIER_PRIORITY.name, 0); } From 27b0b4017b4768cdb7630d27e771baaa5bee29a8 Mon Sep 17 00:00:00 2001 From: Kwantux Date: Sun, 10 Nov 2024 14:39:53 +0100 Subject: [PATCH 06/16] Fix 0 values and duplicates in filter attribute in SortingContainer --- .../component/component/SortingContainer.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/kwantux/networks/component/component/SortingContainer.java b/src/main/java/de/kwantux/networks/component/component/SortingContainer.java index cdd4ff4..14e8959 100755 --- a/src/main/java/de/kwantux/networks/component/component/SortingContainer.java +++ b/src/main/java/de/kwantux/networks/component/component/SortingContainer.java @@ -38,17 +38,20 @@ public static SortingContainer create(BlockLocation pos, PersistentDataContainer int[] filters; try { - filters = container.get(NamespaceUtils.FILTERS.key(), PersistentDataType.INTEGER_ARRAY); // Normal deserialization + filters = Objects.requireNonNull(container.get(NamespaceUtils.FILTERS.key(), PersistentDataType.INTEGER_ARRAY)); // Normal deserialization } catch (Exception e) { - filters = convertLegacyFilters( // If deserialization fails, try legacy deserialization + filters = convertLegacyFilters( // If deserialization fails, try legacy deserialization Objects.requireNonNullElse( - Objects.requireNonNullElse( // Try legacy deserialization + Objects.requireNonNullElse( // Try legacy deserialization container.get(NamespaceUtils.FILTERS.key(), PersistentDataType.STRING), null).split(","), - new String[0]) // If legacy deserialization fails, use empty array + new String[0]) // If legacy deserialization fails, use empty array ); } + // Remove all duplicates and 0 values from filters + filters = Arrays.stream(filters).distinct().filter(i -> i != 0).toArray(); + return new SortingContainer(pos, filters, Objects.requireNonNullElse(container.get(NamespaceUtils.ACCEPTOR_PRIORITY.key(), PersistentDataType.INTEGER), 10), From ba78b1731a6841880d506c9ca7376dddf4ee971b Mon Sep 17 00:00:00 2001 From: Kwantux Date: Sun, 10 Nov 2024 14:40:28 +0100 Subject: [PATCH 07/16] Fix description of component installables --- .../de/kwantux/networks/component/NetworkComponent.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/kwantux/networks/component/NetworkComponent.java b/src/main/java/de/kwantux/networks/component/NetworkComponent.java index 24b52d0..a2329ff 100755 --- a/src/main/java/de/kwantux/networks/component/NetworkComponent.java +++ b/src/main/java/de/kwantux/networks/component/NetworkComponent.java @@ -17,6 +17,7 @@ import org.bukkit.persistence.PersistentDataType; import org.jetbrains.annotations.Nullable; +import java.util.Arrays; import java.util.List; import java.util.Map; @@ -71,7 +72,11 @@ public static ItemStack item(ComponentType type, Map properties) List lore = Main.lang.getItemLore("component." + type.tag()); if (Config.propertyLore) for (Map.Entry entry : properties.entrySet()) { - lore.add(Main.lang.getFinal("property." + entry.getKey()).append(Component.text(": " + entry.getValue()))); + String value = String.valueOf(entry.getValue()); + if (entry.getValue() instanceof int[]) value = Arrays.toString((int[]) entry.getValue()); + if (entry.getValue() instanceof long[]) value = Arrays.toString((long[]) entry.getValue()); + if (entry.getValue() instanceof byte[]) value = Arrays.toString((byte[]) entry.getValue()); + lore.add(Main.lang.getFinal("property." + entry.getKey()).append(Component.text(": " + value))); } meta.lore(lore); } catch (InvalidNodeException e) { From 519f7e19d30cb912b4e725528807a84a776ac268 Mon Sep 17 00:00:00 2001 From: Kwantux Date: Sun, 10 Nov 2024 14:52:02 +0100 Subject: [PATCH 08/16] Fix component.list.empty lang key --- src/main/java/de/kwantux/networks/commands/NetworksCommand.java | 2 +- src/main/resources/lang/de.yml | 1 + src/main/resources/lang/en.yml | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/kwantux/networks/commands/NetworksCommand.java b/src/main/java/de/kwantux/networks/commands/NetworksCommand.java index 0a1a404..339d639 100755 --- a/src/main/java/de/kwantux/networks/commands/NetworksCommand.java +++ b/src/main/java/de/kwantux/networks/commands/NetworksCommand.java @@ -475,7 +475,7 @@ private void components(CommandContext context) { if (network == null) return; if (network.components().isEmpty()) { - lang.message(sender, "component.list.empty"); + lang.message(sender, "component.list.empty", network.name()); return; } diff --git a/src/main/resources/lang/de.yml b/src/main/resources/lang/de.yml index badd156..7bffa42 100755 --- a/src/main/resources/lang/de.yml +++ b/src/main/resources/lang/de.yml @@ -119,6 +119,7 @@ component.exploded: Komponent bei <1> aus dem component.invalid_block: Blöcke vom Typ <1> können keine Netzwerkkomponenten sein! component.nocomponent: Dies ist kein Netzwerkkomponent! component.priority: Priorität wurde auf <1> gesetzt, +component.list.empty: Netzwerk <1> hat keine Komponenten! location.occupied: There is already a component at this location! diff --git a/src/main/resources/lang/en.yml b/src/main/resources/lang/en.yml index 47e2bf5..6a45183 100755 --- a/src/main/resources/lang/en.yml +++ b/src/main/resources/lang/en.yml @@ -120,6 +120,7 @@ component.exploded: NetworkComponent at <1> f component.invalid_block: Blocks of type <1> are not allowed to be network components! component.nocomponent: This block is not a network component! component.priority: Priority set to <1> +component.list.empty: There are no components in network <1>! location.occupied: There is already a component at this location! From 0f9435d00fd6822e6dc4cfd2bc79ac3ef6ad009d Mon Sep 17 00:00:00 2001 From: Kwantux Date: Sun, 10 Nov 2024 14:55:07 +0100 Subject: [PATCH 09/16] Updated lang key rangeupgrade.success to say "component" instead of "network" --- src/main/resources/lang/de.yml | 2 +- src/main/resources/lang/en.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/resources/lang/de.yml b/src/main/resources/lang/de.yml index 7bffa42..5c7a5aa 100755 --- a/src/main/resources/lang/de.yml +++ b/src/main/resources/lang/de.yml @@ -127,7 +127,7 @@ rangeupgrade.last: Du hast bereits die höchste Stufe erreicht. rangeupgrade.alreadyupgraded: Diese Erweiterung hast du bereits installiert, die nächste Stufe ist Telemetrie Antenne Mk.<1> rangeupgrade.unlockfirst: Du musst zuerst Stufe <1> freischalten! rangeupgrade.passivecomponent: Dieses Upgrade ist nur für aktive Komponenten (z.B. Einfüllkisten), jedoch nicht für passiven Komponenten (z.B. Sortierkisten, Restekisten) nutzbar. -rangeupgrade.success: Reichweite <1> wurde im Netzwerk <2> installiert! +rangeupgrade.success: Reichweite <1> wurde am Komponenten <2> installiert! user.add: Spieler <2> wurde zum Netzwerk <1> hinzugefügt user.add.nochange: Spieler <2> wurde bereits zum Netzwerk <1> hinzugefügt diff --git a/src/main/resources/lang/en.yml b/src/main/resources/lang/en.yml index 6a45183..451e1b3 100755 --- a/src/main/resources/lang/en.yml +++ b/src/main/resources/lang/en.yml @@ -128,7 +128,7 @@ rangeupgrade.last: You already reached the maximum upgrade level rangeupgrade.alreadyupgraded: You already installed this upgrade, the next upgrade is range Tier <1> rangeupgrade.unlockfirst: You need to unlock range Tier <1> first! rangeupgrade.passivecomponent: This can only be applied to active network components (e.g. Input), NOT passive components (e.g. Sorting, Misc) -rangeupgrade.success: Successfully upgraded network <2> to Tier <1>! +rangeupgrade.success: Successfully upgraded component <2> to Tier <1>! user.add: Successfully added <2> to the network <1> user.remove: Successfully removed <2> from the network <1> From d6ee6034ae87fbfd981459745f8da8006ca360e8 Mon Sep 17 00:00:00 2001 From: Kwantux Date: Sun, 10 Nov 2024 15:14:25 +0100 Subject: [PATCH 10/16] Made it that clicking components with the network wand works no matter if it's the actual component or the vanilla chest attached to it --- .../java/de/kwantux/networks/event/WandListener.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/kwantux/networks/event/WandListener.java b/src/main/java/de/kwantux/networks/event/WandListener.java index 3f8b1af..eb89659 100644 --- a/src/main/java/de/kwantux/networks/event/WandListener.java +++ b/src/main/java/de/kwantux/networks/event/WandListener.java @@ -83,13 +83,16 @@ public void onPlayerInteract(PlayerInteractEvent event) throws InvalidNodeExcept } if (l == null) return; - Network network = net.getNetworkWithComponent(l); - NetworkComponent component = net.getComponent(l); + NetworkComponent component = dcu.componentAt(l); + Network network = null; + if (component != null) network = mgr.getNetworkWithComponent(component.pos()); if (!p.isSneaking()) { if (action.equals(Action.RIGHT_CLICK_BLOCK)) { - p.sendMessage(NetworksCommand.componentInfo(network, component)); - if(component instanceof SortingContainer container) p.sendMessage(Arrays.toString(container.filters())); + if (component != null) + p.sendMessage(NetworksCommand.componentInfo(network, component)); + else + lang.message(p, "component.nocomponent"); } return; } From 59906708eee030f1f79695ef2c8ce1c54053c118 Mon Sep 17 00:00:00 2001 From: Kwantux Date: Sun, 10 Nov 2024 15:14:54 +0100 Subject: [PATCH 11/16] Replaced object references to controllers with static references --- src/main/java/de/kwantux/networks/Main.java | 6 +-- .../networks/commands/NetworksCommand.java | 5 -- .../networks/event/BlockBreakListener.java | 29 ++++------- .../networks/event/BlockPlaceListener.java | 27 +++------- .../kwantux/networks/event/WandListener.java | 52 +++++++------------ 5 files changed, 39 insertions(+), 80 deletions(-) diff --git a/src/main/java/de/kwantux/networks/Main.java b/src/main/java/de/kwantux/networks/Main.java index 3b6f279..76cccab 100755 --- a/src/main/java/de/kwantux/networks/Main.java +++ b/src/main/java/de/kwantux/networks/Main.java @@ -122,9 +122,9 @@ public void onEnable() { mgr.loadData(); new ComponentListener(this); - new BlockPlaceListener(this, dcu); - new BlockBreakListener(this, dcu); - new WandListener(this, crf, dcu); + new BlockPlaceListener(this); + new BlockBreakListener(this); + new WandListener(this); new PlayerJoinListener(this); if (FoliaUtils.folia) { diff --git a/src/main/java/de/kwantux/networks/commands/NetworksCommand.java b/src/main/java/de/kwantux/networks/commands/NetworksCommand.java index 339d639..aeb632b 100755 --- a/src/main/java/de/kwantux/networks/commands/NetworksCommand.java +++ b/src/main/java/de/kwantux/networks/commands/NetworksCommand.java @@ -480,11 +480,6 @@ private void components(CommandContext context) { } for (NetworkComponent component : network.components()) { - System.out.println(component); - System.out.println(component.type()); - System.out.println(component.pos()); - System.out.println(component.type().tag()); - sender.sendMessage( Objects.requireNonNullElse(lang.getFinal("item.name.component."+component.type().tag()), Component.text("Unknown Component type: " + component.type().tag())) .append(Component.text(": ") diff --git a/src/main/java/de/kwantux/networks/event/BlockBreakListener.java b/src/main/java/de/kwantux/networks/event/BlockBreakListener.java index b05221c..fa02b9b 100755 --- a/src/main/java/de/kwantux/networks/event/BlockBreakListener.java +++ b/src/main/java/de/kwantux/networks/event/BlockBreakListener.java @@ -1,13 +1,10 @@ package de.kwantux.networks.event; import de.kwantux.networks.Main; -import de.kwantux.networks.Manager; import de.kwantux.networks.Network; import de.kwantux.networks.component.NetworkComponent; import de.kwantux.networks.config.Config; import de.kwantux.networks.utils.BlockLocation; -import de.kwantux.networks.utils.DoubleChestUtils; -import de.kwantux.config.lang.LanguageController; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.block.Block; @@ -22,35 +19,31 @@ import java.util.List; import java.util.UUID; -public class BlockBreakListener implements Listener { - - private final Manager manager; - private final DoubleChestUtils dcu; - private final LanguageController lang; +import static de.kwantux.networks.Main.dcu; +import static de.kwantux.networks.Main.mgr; +import static de.kwantux.networks.Main.lang; +public class BlockBreakListener implements Listener { - public BlockBreakListener(Main main, DoubleChestUtils doubleChestDisconnecter) { + public BlockBreakListener(Main main) { main.getServer().getPluginManager().registerEvents(this, main); - manager = main.getNetworkManager(); - lang = main.getLanguage(); - dcu = doubleChestDisconnecter; } @EventHandler (priority = EventPriority.HIGHEST) public void blockBreak(BlockBreakEvent event) { - for (Network network : manager.getNetworks()) { + for (Network network : mgr.getNetworks()) { for (NetworkComponent component : List.copyOf(network.components())) { if (component.pos().equals(new BlockLocation(event.getBlock()))) { dcu.disconnectChests(component.pos()); - if (manager.permissionUser(event.getPlayer(), network)) { + if (mgr.permissionUser(event.getPlayer(), network)) { ItemStack item = component.item(); Bukkit.getServer().getWorld(component.pos().getWorld()).dropItem(component.pos().getBukkitLocation(), item); BlockLocation location = new BlockLocation(event.getBlock()); - manager.removeComponent(location); + mgr.removeComponent(location); lang.message(event.getPlayer(), "component.remove", location.toString()); } @@ -69,14 +62,14 @@ public void onBlockBreak(EntityExplodeEvent event) { ArrayList removeLater = new ArrayList<>(); for (Block block : event.blockList()) { - if (manager.getComponent(new BlockLocation(block)) != null) { + if (mgr.getComponent(new BlockLocation(block)) != null) { removeLater.add(block); } } for (Block block : removeLater) { if (!Config.blastProofComponents) { - NetworkComponent component = manager.getComponent(new BlockLocation(block)); + NetworkComponent component = mgr.getComponent(new BlockLocation(block)); assert component != null; // Was already checked when adding blocks to the list ItemStack item = component.item(); @@ -84,7 +77,7 @@ public void onBlockBreak(EntityExplodeEvent event) { event.blockList().remove(block); block.setType(Material.AIR); - Network network = manager.getNetworkWithComponent(new BlockLocation(block)); + Network network = mgr.getNetworkWithComponent(new BlockLocation(block)); network.removeComponent(new BlockLocation(block)); ArrayList users = (ArrayList) network.users(); users.add(network.owner()); diff --git a/src/main/java/de/kwantux/networks/event/BlockPlaceListener.java b/src/main/java/de/kwantux/networks/event/BlockPlaceListener.java index 1cd8314..7d0afac 100644 --- a/src/main/java/de/kwantux/networks/event/BlockPlaceListener.java +++ b/src/main/java/de/kwantux/networks/event/BlockPlaceListener.java @@ -1,14 +1,11 @@ package de.kwantux.networks.event; import de.kwantux.networks.Main; -import de.kwantux.networks.Manager; import de.kwantux.networks.Network; import de.kwantux.networks.component.ComponentType; -import de.kwantux.networks.config.Config; import de.kwantux.networks.utils.BlockLocation; import de.kwantux.networks.utils.DoubleChestUtils; import de.kwantux.networks.utils.NamespaceUtils; -import de.kwantux.config.lang.LanguageController; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -20,24 +17,14 @@ import org.bukkit.persistence.PersistentDataContainer; import org.bukkit.persistence.PersistentDataType; +import static de.kwantux.networks.Main.cfg; +import static de.kwantux.networks.Main.lang; import static de.kwantux.networks.Main.mgr; public class BlockPlaceListener implements Listener { - private final Manager net; - private final Config config; - private final LanguageController lang; - - private final DoubleChestUtils dcd; - - public BlockPlaceListener (Main plugin, DoubleChestUtils doubleChestDisconnecter) { + public BlockPlaceListener (Main plugin) { plugin.getServer().getPluginManager().registerEvents(this, plugin); - - this.net = plugin.getNetworkManager(); - this.config = plugin.getConfiguration(); - this.lang = plugin.getLanguage(); - - dcd = doubleChestDisconnecter; } @EventHandler(priority= EventPriority.HIGH) @@ -46,7 +33,7 @@ public void onBlockPlace(BlockPlaceEvent event) { Player p = event.getPlayer(); BlockLocation pos = new BlockLocation(event.getBlock()); - Network network = net.selection(p); + Network network = mgr.selection(p); ItemStack item = event.getItemInHand().clone(); PersistentDataContainer container = item.getItemMeta().getPersistentDataContainer(); @@ -55,7 +42,7 @@ public void onBlockPlace(BlockPlaceEvent event) { if (type == null) return; - if (config.checkLocation(pos, type)) { + if (cfg.checkLocation(pos, type)) { if (network == null) { lang.message(p, "select.noselection"); @@ -78,7 +65,7 @@ public void onComponentInstall(PlayerInteractEvent event) { Player p = event.getPlayer(); BlockLocation pos = new BlockLocation(event.getClickedBlock()); - Network network = net.selection(p); + Network network = mgr.selection(p); ItemStack item = event.getItem(); if (item == null) return; @@ -95,7 +82,7 @@ public void onComponentInstall(PlayerInteractEvent event) { return; } - if (config.checkLocation(pos, type)) { // Check if the block can actually be a component (e.g. grass blocks cannot be input containers, but chests and barrels can) + if (cfg.checkLocation(pos, type)) { // Check if the block can actually be a component (e.g. grass blocks cannot be input containers, but chests and barrels can) if (network == null) { lang.message(p, "select.noselection"); diff --git a/src/main/java/de/kwantux/networks/event/WandListener.java b/src/main/java/de/kwantux/networks/event/WandListener.java index eb89659..1768c7c 100644 --- a/src/main/java/de/kwantux/networks/event/WandListener.java +++ b/src/main/java/de/kwantux/networks/event/WandListener.java @@ -2,22 +2,17 @@ import de.kwantux.config.util.exceptions.InvalidNodeException; import de.kwantux.networks.Main; -import de.kwantux.networks.Manager; import de.kwantux.networks.Network; import de.kwantux.networks.commands.NetworksCommand; import de.kwantux.networks.component.NetworkComponent; -import de.kwantux.networks.component.component.InputContainer; import de.kwantux.networks.component.component.SortingContainer; import de.kwantux.networks.component.module.Acceptor; import de.kwantux.networks.component.module.Donator; import de.kwantux.networks.component.module.Requestor; import de.kwantux.networks.component.util.FilterTranslator; -import de.kwantux.networks.config.Config; import de.kwantux.networks.config.CraftingManager; import de.kwantux.networks.utils.BlockLocation; import de.kwantux.networks.utils.DoubleChestUtils; -import de.kwantux.config.lang.LanguageController; -import de.kwantux.networks.component.component.MiscContainer; import net.kyori.adventure.key.Key; import net.kyori.adventure.text.event.HoverEvent; import org.bukkit.Material; @@ -31,24 +26,16 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.persistence.PersistentDataType; -import java.util.Arrays; +import static de.kwantux.networks.Main.lang; +import static de.kwantux.networks.Main.dcu; +import static de.kwantux.networks.Main.cfg; +import static de.kwantux.networks.Main.crf; +import static de.kwantux.networks.Main.mgr; public class WandListener implements Listener { - private final Config config; - private final Manager net; - private final LanguageController lang; - private final CraftingManager crafting; - private final DoubleChestUtils dcu; - - public WandListener(Main plugin, CraftingManager craftingManager, DoubleChestUtils dcu) { + public WandListener(Main plugin) { plugin.getServer().getPluginManager().registerEvents(this, plugin); - - this.config = plugin.getConfiguration(); - this.net = plugin.getNetworkManager(); - this.lang = plugin.getLanguage(); - this.crafting = craftingManager; - this.dcu = dcu; } @EventHandler @@ -68,14 +55,14 @@ public void onPlayerInteract(PlayerInteractEvent event) throws InvalidNodeExcept if (!event.getHand().equals(EquipmentSlot.HAND)) return; int mode = wand.getItemMeta().getPersistentDataContainer().get(new NamespacedKey("networks", "wand"), PersistentDataType.INTEGER); - + if (!p.isSneaking()) { if (action.equals(Action.LEFT_CLICK_BLOCK) || action.equals(Action.LEFT_CLICK_AIR)) { if (!p.isSneaking()) { mode++; if (mode > 2) mode = 0; //p.getInventory().setItemInMainHand(crf.getNetworkWand(mode)); - event.getItem().setItemMeta(crafting.getNetworkWand(mode).getItemMeta()); + event.getItem().setItemMeta(crf.getNetworkWand(mode).getItemMeta()); lang.message(p, "wand.mode", lang.getRaw("wand.mode." + mode)); return; } @@ -106,7 +93,7 @@ public void onPlayerInteract(PlayerInteractEvent event) throws InvalidNodeExcept return; } - if (!net.permissionUser(p, network)) { + if (!mgr.permissionUser(p, network)) { lang.message(p, "permission.user"); return; } @@ -114,11 +101,10 @@ public void onPlayerInteract(PlayerInteractEvent event) throws InvalidNodeExcept ItemStack itemInOffHand = p.getInventory().getItemInOffHand(); if (action.equals(Action.RIGHT_CLICK_BLOCK)) { - if (mode == 0 && !itemInOffHand.getType().equals(Material.AIR) && net.getComponent(l) instanceof SortingContainer) { - NetworkComponent c = net.getComponent(l); + if (mode == 0 && !itemInOffHand.getType().equals(Material.AIR) && mgr.getComponent(l) instanceof SortingContainer) { + NetworkComponent c = mgr.getComponent(l); if (c instanceof SortingContainer container) { container.addFilter(itemInOffHand.getType().ordinal()); - p.sendMessage(Arrays.toString(container.filters())); lang.message(p, "component.sorting.setitem", l.toString(), itemInOffHand.getType().toString()); } } @@ -128,8 +114,8 @@ public void onPlayerInteract(PlayerInteractEvent event) throws InvalidNodeExcept lang.message(p, "component.priority", String.valueOf(container.acceptorPriority())); } } - if (mode == 2 && !itemInOffHand.getType().equals(Material.AIR) && net.getComponent(l) instanceof SortingContainer) { - NetworkComponent c = net.getComponent(l); + if (mode == 2 && !itemInOffHand.getType().equals(Material.AIR) && mgr.getComponent(l) instanceof SortingContainer) { + NetworkComponent c = mgr.getComponent(l); if (c instanceof SortingContainer container) { int hash = itemInOffHand.getItemMeta().hashCode(); container.addFilter(hash); @@ -138,7 +124,6 @@ public void onPlayerInteract(PlayerInteractEvent event) throws InvalidNodeExcept Key.key(itemInOffHand.getType().name().toLowerCase()), 1 ) ))); - p.sendMessage(Arrays.toString(container.filters())); lang.message(p, "component.sorting.setitem", l.displayText(), itemInOffHand.displayName()); } } @@ -146,13 +131,12 @@ public void onPlayerInteract(PlayerInteractEvent event) throws InvalidNodeExcept if (action.equals(Action.LEFT_CLICK_BLOCK)) { - if ((mode == 0 || mode == 2) && net.getComponent(l) instanceof SortingContainer && !itemInOffHand.getType().equals(Material.AIR) && p.isSneaking()) { - NetworkComponent c = net.getComponent(l); + if ((mode == 0 || mode == 2) && mgr.getComponent(l) instanceof SortingContainer && !itemInOffHand.getType().equals(Material.AIR) && p.isSneaking()) { + NetworkComponent c = mgr.getComponent(l); if (c instanceof SortingContainer container) { int hash = itemInOffHand.getItemMeta().hashCode(); container.removeFilter(itemInOffHand.getType().ordinal()); container.removeFilter(hash); - p.sendMessage(Arrays.toString(container.filters())); lang.message(p, "component.sorting.removeitem", l.displayText(), itemInOffHand.displayName()); } } @@ -169,7 +153,7 @@ public void onPlayerInteract(PlayerInteractEvent event) throws InvalidNodeExcept if (p.getInventory().getItemInMainHand().getItemMeta().getPersistentDataContainer().has(new NamespacedKey("networks", "upgrade.range"), PersistentDataType.INTEGER)) { if (action.equals(Action.RIGHT_CLICK_BLOCK)) { event.setCancelled(true); - NetworkComponent component = net.getComponent(l); + NetworkComponent component = mgr.getComponent(l); if (component == null) { lang.message(p, "component.nocomponent"); return; @@ -184,7 +168,7 @@ public void onPlayerInteract(PlayerInteractEvent event) throws InvalidNodeExcept donator.rangeUp(); lang.message(p, "rangeupgrade.success", String.valueOf(tier+1), component.pos().toString()); } - if (tier == config.getMaxRanges().length) { + if (tier == cfg.getMaxRanges().length) { lang.message(p, "rangeupgrade.last"); return; } @@ -205,7 +189,7 @@ else if (component instanceof Requestor requestor) { requestor.rangeUp(); lang.message(p, "rangeupgrade.success", String.valueOf(tier), component.pos().toString()); } - if (tier == config.getMaxRanges().length) { + if (tier == cfg.getMaxRanges().length) { lang.message(p, "rangeupgrade.last"); return; } From 9c0c2ca4c4982668322c897bcf234272d5d4801f Mon Sep 17 00:00:00 2001 From: Kwantux Date: Sun, 10 Nov 2024 15:39:44 +0100 Subject: [PATCH 12/16] Added auto-saves --- src/main/java/de/kwantux/networks/Main.java | 4 ++++ src/main/java/de/kwantux/networks/Manager.java | 1 - src/main/java/de/kwantux/networks/config/Config.java | 6 ++++++ src/main/resources/general.conf | 9 +++++---- 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/main/java/de/kwantux/networks/Main.java b/src/main/java/de/kwantux/networks/Main.java index 76cccab..63e3028 100755 --- a/src/main/java/de/kwantux/networks/Main.java +++ b/src/main/java/de/kwantux/networks/Main.java @@ -25,6 +25,7 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; +import java.util.concurrent.TimeUnit; import java.util.logging.Logger; @@ -137,6 +138,9 @@ public void onEnable() { } } + if (Config.autoSaveInterval > 0) + asyncScheduler.runAtFixedRate(this, (t) -> mgr.saveData(), Config.autoSaveInterval, Config.autoSaveInterval, TimeUnit.SECONDS); + if (cfg.logoOnLaunch()) logger.info(startMessage); } diff --git a/src/main/java/de/kwantux/networks/Manager.java b/src/main/java/de/kwantux/networks/Manager.java index f67d344..f53e920 100755 --- a/src/main/java/de/kwantux/networks/Manager.java +++ b/src/main/java/de/kwantux/networks/Manager.java @@ -104,7 +104,6 @@ public void saveData() { for (String id : networks.keySet()) { storage.saveNetwork(id, networks.get(id)); } - logger.info("Saved " + networks.size() + " Networks"); try { FilterTranslator.save(); } catch (IOException e) { diff --git a/src/main/java/de/kwantux/networks/config/Config.java b/src/main/java/de/kwantux/networks/config/Config.java index 51ec2cb..45b83f6 100755 --- a/src/main/java/de/kwantux/networks/config/Config.java +++ b/src/main/java/de/kwantux/networks/config/Config.java @@ -44,6 +44,7 @@ public Config(Main main) throws SerializationException { config.require("requestOwnershipTransfers"); config.require("humanReadableJson"); config.require("archiveNetworksOnDelete"); + config.require("autoSave"); ConfigurationTransformers.generalConfigTransformers(config); @@ -57,6 +58,7 @@ public Config(Main main) throws SerializationException { complexInventoryChecks = config.getFinalBoolean("performance.complexInventoryChecks"); propertyLore = config.getFinalBoolean("propertyLore"); loadChunks = config.getFinalBoolean("performance.loadChunks"); + autoSaveInterval = config.getFinalInt("autoSave"); commands = config.getFinalList("commands", String.class).toArray(new String[0]); } catch (ConfigAlreadyRegisteredException e) { @@ -72,6 +74,10 @@ public Config(Main main) throws SerializationException { public static boolean complexInventoryChecks; public static boolean propertyLore; public static boolean loadChunks; + /** + * Auto save interval in seconds + */ + public static int autoSaveInterval; public static String[] commands; diff --git a/src/main/resources/general.conf b/src/main/resources/general.conf index 9419e43..690b03e 100755 --- a/src/main/resources/general.conf +++ b/src/main/resources/general.conf @@ -10,14 +10,15 @@ commands = [ "n", "net", "network" ] # Only works for blocks that have inventories component { input = [ "CHEST", "TRAPPED_CHEST", "BARREL", "HOPPER", "DISPENSER", "DROPPER" ] - #filtered_input = [ "CHEST", "TRAPPED_CHEST", "BARREL", "HOPPER", "DISPENSER", "DROPPER" ] - #nbt_filtered_input = [ "CHEST", "TRAPPED_CHEST", "BARREL", "HOPPER", "DISPENSER", "DROPPER" ] sorting = [ "CHEST", "TRAPPED_CHEST", "BARREL", "HOPPER", "DISPENSER", "DROPPER" ] - #nbt_sorting = [ "CHEST", "TRAPPED_CHEST", "BARREL", "HOPPER", "DISPENSER", "DROPPER" ] misc = [ "CHEST", "TRAPPED_CHEST", "BARREL", "HOPPER", "DISPENSER", "DROPPER" ] #furnace = [ "furnace", "blast_furnace", "smoke_furnace" ] } +# Auto-save interval in seconds +# Set to 0 to disable auto save +autoSave = 120 + # Performance related options # Changing these can impact your server's performance negatively in some cases performance { @@ -29,6 +30,7 @@ performance { # Load chunks of connected network components # Guarantees that items can be transmitted as far as you want # May SIGNIFICANTLY reduce your server's performance on LARGE servers! Use with caution + # On Folia servers this does not always work since item transmission must happen within one tick region loadChunks = false } @@ -41,7 +43,6 @@ material { # Default properties for networks - properties { baseRange = 20 maxComponents = -1 From 9b8beda10bcd8b4f03df40e5f45870c075b0ef5b Mon Sep 17 00:00:00 2001 From: Kwantux Date: Sun, 10 Nov 2024 15:39:55 +0100 Subject: [PATCH 13/16] Delete redundant file --- src/main/resources/lang/commands/en.yml | 21 --------------------- 1 file changed, 21 deletions(-) delete mode 100644 src/main/resources/lang/commands/en.yml diff --git a/src/main/resources/lang/commands/en.yml b/src/main/resources/lang/commands/en.yml deleted file mode 100644 index 6779a4b..0000000 --- a/src/main/resources/lang/commands/en.yml +++ /dev/null @@ -1,21 +0,0 @@ -networks: - "@": - - networks - - network - - net - - n - - help: help - manual: manual - - create: create - rename: rename - delete: delete - delete.confirm: confirm - select: select - - list: list - listall: listall - user: user - items: items - merge: merge \ No newline at end of file From 8f048362f37204965f3174f57bf30edcda51d800 Mon Sep 17 00:00:00 2001 From: Kwantux Date: Sun, 10 Nov 2024 15:41:46 +0100 Subject: [PATCH 14/16] Changed color of strict filter mode for the network wand from blue to dark green --- src/main/resources/lang/de.yml | 2 +- src/main/resources/lang/en.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/resources/lang/de.yml b/src/main/resources/lang/de.yml index 5c7a5aa..683c4a5 100755 --- a/src/main/resources/lang/de.yml +++ b/src/main/resources/lang/de.yml @@ -163,7 +163,7 @@ items.message: "Alle Items im Netzwerk <1>:\n( item.name.wand0: Netzwerk Konfigurator [Filter] item.name.wand1: Netzwerk Konfigurator [Priorität] -item.name.wand2: Netzwerk Konfigurator [Filter (Strikt)] +item.name.wand2: Netzwerk Konfigurator [Filter (Strikt)] item.name.component.input: Einfüllkiste item.name.component.sorting: Sortierkiste item.name.component.misc: Restekiste diff --git a/src/main/resources/lang/en.yml b/src/main/resources/lang/en.yml index 451e1b3..9e75c40 100755 --- a/src/main/resources/lang/en.yml +++ b/src/main/resources/lang/en.yml @@ -165,7 +165,7 @@ items.message: "Total items in network <1>:\n( item.name.wand0: Network Wand [Filter] item.name.wand1: Network Wand [Priority] -item.name.wand2: Network Wand [Strict Filter] +item.name.wand2: Network Wand [Strict Filter] item.name.component.input: Input Container item.name.component.sorting: Sorting Container item.name.component.misc: Miscellaneous Container From f64d79d8fdf67f6e00c91d898ba7cd0859e3ad05 Mon Sep 17 00:00:00 2001 From: Kwantux Date: Sun, 10 Nov 2024 15:42:07 +0100 Subject: [PATCH 15/16] Update 3.0.5 --- build.gradle.kts | 2 +- src/main/resources/README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index fe87a3e..0afa1fb 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -12,7 +12,7 @@ plugins { runPaper.folia.registerTask() group = "de.kwantux" -version = "3.0.4" +version = "3.0.5" description = "A performance friendly way to sort your items" repositories { diff --git a/src/main/resources/README.md b/src/main/resources/README.md index 3e50799..4cef349 100755 --- a/src/main/resources/README.md +++ b/src/main/resources/README.md @@ -1,4 +1,4 @@ -# Networks v3.0.4 +# Networks v3.0.5 ## Report any bugs on Discord or Github From 4d4056e068ca42869569cf7078ff89ace6388213 Mon Sep 17 00:00:00 2001 From: Kwantux Date: Sun, 10 Nov 2024 16:16:30 +0100 Subject: [PATCH 16/16] Readded the notice feature --- src/main/java/de/kwantux/networks/Main.java | 2 ++ .../de/kwantux/networks/config/Config.java | 5 ++- .../networks/event/NoticeListener.java | 32 +++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100755 src/main/java/de/kwantux/networks/event/NoticeListener.java diff --git a/src/main/java/de/kwantux/networks/Main.java b/src/main/java/de/kwantux/networks/Main.java index 63e3028..578c0de 100755 --- a/src/main/java/de/kwantux/networks/Main.java +++ b/src/main/java/de/kwantux/networks/Main.java @@ -2,6 +2,7 @@ import de.kwantux.networks.commands.NetworksCommandManager; import de.kwantux.networks.component.util.FilterTranslator; +import de.kwantux.networks.event.NoticeListener; import de.kwantux.networks.utils.DoubleChestUtils; import de.kwantux.networks.utils.FoliaUtils; import de.kwantux.networks.utils.Metrics; @@ -127,6 +128,7 @@ public void onEnable() { new BlockBreakListener(this); new WandListener(this); new PlayerJoinListener(this); + new NoticeListener(this); if (FoliaUtils.folia) { logger.warning("Folia support on Networks is still in beta, please report any bugs."); diff --git a/src/main/java/de/kwantux/networks/config/Config.java b/src/main/java/de/kwantux/networks/config/Config.java index 45b83f6..be84cb2 100755 --- a/src/main/java/de/kwantux/networks/config/Config.java +++ b/src/main/java/de/kwantux/networks/config/Config.java @@ -67,7 +67,10 @@ public Config(Main main) throws SerializationException { } public static boolean blastProofComponents; // TODO: Blast proof components - public static boolean noticeEnabled; // TODO: Notice message + /** + * Whether players that don't have a network should get a notification when opening a full chest + */ + public static boolean noticeEnabled; public static boolean humanReadableJson; public static boolean archiveNetworksOnDelete; public static boolean requestOwnershipTransfers; diff --git a/src/main/java/de/kwantux/networks/event/NoticeListener.java b/src/main/java/de/kwantux/networks/event/NoticeListener.java new file mode 100755 index 0000000..ceaea23 --- /dev/null +++ b/src/main/java/de/kwantux/networks/event/NoticeListener.java @@ -0,0 +1,32 @@ +package de.kwantux.networks.event; + +import de.kwantux.networks.Main; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.inventory.InventoryCloseEvent; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +import static de.kwantux.networks.Main.mgr; +import static de.kwantux.networks.Main.lang; + +public class NoticeListener implements Listener { + + private List notices = new ArrayList<>(); + + public NoticeListener(Main plugin) { + plugin.getServer().getPluginManager().registerEvents(this, plugin); + } + + @EventHandler + public void onInventoryOpen(InventoryCloseEvent event) { + if (event.getInventory().firstEmpty() != -1) return; + if (notices.contains(event.getPlayer().getUniqueId())) return; + if (mgr.withUser(event.getPlayer().getUniqueId()).isEmpty()) { + notices.add(event.getPlayer().getUniqueId()); + lang.message(event.getPlayer(), "notice"); + } + } +}