diff --git a/.github/workflows/package.yml b/.github/workflows/package.yml deleted file mode 100644 index 197d1b27..00000000 --- a/.github/workflows/package.yml +++ /dev/null @@ -1,24 +0,0 @@ -name: Publish package to GitHub Packages -on: - push: - branches: - - master - pull_request: - branches: - - master -jobs: - publish: - runs-on: ubuntu-latest - permissions: - contents: read - packages: write - steps: - - uses: actions/checkout@v2 - - uses: actions/setup-java@v2 - with: - java-version: '16' - distribution: 'temurin' - - name: Publish package - run: mvn --batch-mode deploy - env: - GITHUB_TOKEN: ${{ secrets.TOKEN }} \ No newline at end of file diff --git a/.npmrc b/.npmrc deleted file mode 100644 index 68f0bcec..00000000 --- a/.npmrc +++ /dev/null @@ -1 +0,0 @@ -@sefiraat:registry=https://npm.pkg.github.com \ No newline at end of file diff --git a/dependency-reduced-pom.xml b/dependency-reduced-pom.xml index 9c8d417d..bd9a63a3 100644 --- a/dependency-reduced-pom.xml +++ b/dependency-reduced-pom.xml @@ -1,140 +1,139 @@ - - 4.0.0 - io.github.sefiraat - networks - MODIFIED_1.0.1 - - ${basedir}/src/main/java - clean package - - - true - ${basedir}/src/main/resources - - **/*.* - tags/*.json - - - - ${project.name} v${project.version} - - - maven-shade-plugin - 3.3.1-SNAPSHOT - - - package - - shade - - - - - - - org.bstats - io.github.sefiraat.alone.bstats - - - - - *:* - - META-INF/* - - - - - - - - - - spigot-repo - https://hub.spigotmc.org/nexus/content/repositories/snapshots/ - - - jitpack.io - https://jitpack.io - - - jeff-media-public - https://hub.jeff-media.com/nexus/repository/jeff-media-public/ - - - - - - never - - - snapshots - maven-snapshots - https://repository.apache.org/content/repositories/snapshots/ - - - - never - - - central - Maven Plugin Repository - https://repo1.maven.org/maven2 - - - - - org.spigotmc - spigot-api - 1.17.1-R0.1-SNAPSHOT - provided - - - com.github.Slimefun - Slimefun4 - b8f7dc - provided - - - org.projectlombok - lombok - 1.18.20 - provided - - - io.github.mooy1 - InfinityExpansion - 90e11bc - provided - - - - - central - sefi-central - https://sefiraat.jfrog.io/artifactory/default-maven-remote - - - snapshots - sefi-snapshot - https://sefiraat.jfrog.io/artifactory/default-maven-remote - - - - java:S2245 - ** - e1,e2,e3 - 16 - java:S125 - https://sonarcloud.io - sefiraat - Crystamae_Historia - 16 - UTF-8 - ** - ** - java:S6212 - + + 4.0.0 + io.github.sefiraat + networks + MODIFIED_1.0.2 + + ${basedir}/src/main/java + clean package + + + true + ${basedir}/src/main/resources + + **/*.* + tags/*.json + + + + ${project.name} v${project.version} + + + maven-shade-plugin + 3.3.1-SNAPSHOT + + + package + + shade + + + + + + + org.bstats + io.github.sefiraat.alone.bstats + + + + + *:* + + META-INF/* + + + + + + + + + + spigot-repo + https://hub.spigotmc.org/nexus/content/repositories/snapshots/ + + + jitpack.io + https://jitpack.io + + + jeff-media-public + https://hub.jeff-media.com/nexus/repository/jeff-media-public/ + + + + + + never + + + snapshots + maven-snapshots + https://repository.apache.org/content/repositories/snapshots/ + + + + never + + + central + Maven Plugin Repository + https://repo1.maven.org/maven2 + + + + + org.spigotmc + spigot-api + 1.17.1-R0.1-SNAPSHOT + provided + + + com.github.Slimefun + Slimefun4 + b8f7dc + provided + + + org.projectlombok + lombok + 1.18.20 + provided + + + io.github.mooy1 + InfinityExpansion + 90e11bc + provided + + + + + central + sefi-central + https://sefiraat.jfrog.io/artifactory/default-maven-local + + + snapshots + sefi-snapshot + https://sefiraat.jfrog.io/artifactory/default-maven-local + + + + java:S2245 + ** + e1,e2,e3 + 16 + java:S125 + https://sonarcloud.io + sefiraat + Crystamae_Historia + 16 + UTF-8 + ** + ** + java:S6212 + diff --git a/pom.xml b/pom.xml index 771ad822..f9e698a9 100644 --- a/pom.xml +++ b/pom.xml @@ -6,18 +6,18 @@ 4.0.0 io.github.sefiraat networks - MODIFIED_1.0.1 + MODIFIED_1.0.2 central sefi-central - https://sefiraat.jfrog.io/artifactory/default-maven-remote + https://sefiraat.jfrog.io/artifactory/default-maven-local snapshots sefi-snapshot - https://sefiraat.jfrog.io/artifactory/default-maven-remote + https://sefiraat.jfrog.io/artifactory/default-maven-local diff --git a/src/main/java/io/github/sefiraat/networks/NetworkStorage.java b/src/main/java/io/github/sefiraat/networks/NetworkStorage.java index 74a53dc0..c87d6d22 100644 --- a/src/main/java/io/github/sefiraat/networks/NetworkStorage.java +++ b/src/main/java/io/github/sefiraat/networks/NetworkStorage.java @@ -1,5 +1,6 @@ package io.github.sefiraat.networks; +import io.github.sefiraat.networks.network.NetworkNode; import io.github.sefiraat.networks.network.NodeDefinition; import lombok.experimental.UtilityClass; import org.bukkit.Location; @@ -10,10 +11,27 @@ @UtilityClass public class NetworkStorage { - protected static final Map ALL_NETWORK_OBJECTS = new HashMap<>(); + private static final Map ALL_NETWORK_OBJECTS = new HashMap<>(); + + public static void removeNode(Location location) { + final NodeDefinition nodeDefinition = ALL_NETWORK_OBJECTS.remove(location); + + if (nodeDefinition == null) { + return; + } + + final NetworkNode node = nodeDefinition.getNode(); + + if (node == null) { + return; + } + + for (NetworkNode childNode : nodeDefinition.getNode().getChildrenNodes()) { + removeNode(childNode.getNodePosition()); + } + } public static Map getAllNetworkObjects() { return ALL_NETWORK_OBJECTS; } - } diff --git a/src/main/java/io/github/sefiraat/networks/managers/ListenerManager.java b/src/main/java/io/github/sefiraat/networks/managers/ListenerManager.java index 21479341..1a3b20fc 100644 --- a/src/main/java/io/github/sefiraat/networks/managers/ListenerManager.java +++ b/src/main/java/io/github/sefiraat/networks/managers/ListenerManager.java @@ -13,5 +13,4 @@ public ListenerManager() { private void addListener(Listener listener) { Networks.getPluginManager().registerEvents(listener, Networks.getInstance()); } - } diff --git a/src/main/java/io/github/sefiraat/networks/network/ItemRequest.java b/src/main/java/io/github/sefiraat/networks/network/ItemRequest.java index 4a4c8a42..b2e2b3ee 100644 --- a/src/main/java/io/github/sefiraat/networks/network/ItemRequest.java +++ b/src/main/java/io/github/sefiraat/networks/network/ItemRequest.java @@ -2,22 +2,19 @@ import org.bukkit.inventory.ItemStack; -public class ItemRequest { +import javax.annotation.Nonnull; + +public class ItemRequest extends ItemStackCache { - private final ItemStack itemStack; private int amount; - public ItemRequest(ItemStack itemStack, int amount) { - this.itemStack = itemStack; + public ItemRequest(@Nonnull ItemStack itemStack, int amount) { + super(itemStack); this.amount = amount; } - public ItemStack getItemStack() { - return itemStack; - } - public int getAmount() { - return amount; + return this.amount; } public void setAmount(int amount) { diff --git a/src/main/java/io/github/sefiraat/networks/network/ItemStackCache.java b/src/main/java/io/github/sefiraat/networks/network/ItemStackCache.java new file mode 100644 index 00000000..bf7d1218 --- /dev/null +++ b/src/main/java/io/github/sefiraat/networks/network/ItemStackCache.java @@ -0,0 +1,49 @@ +package io.github.sefiraat.networks.network; + +import org.bukkit.Material; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; + +import javax.annotation.Nullable; + +public class ItemStackCache { + + private ItemStack itemStack; + @Nullable + private ItemMeta itemMeta = null; + @Nullable + private Material itemType = null; + + public ItemStackCache(@Nullable ItemStack itemStack) { + this.itemStack = itemStack; + if (itemStack != null) { + this.itemMeta = itemStack.hasItemMeta() ? itemStack.getItemMeta() : null; + this.itemType = itemStack.getType(); + } + } + + public ItemStackCache(@Nullable ItemStack itemStack, @Nullable ItemMeta itemMeta, @Nullable Material itemType) { + this.itemStack = itemStack; + this.itemMeta = itemMeta; + this.itemType = itemType; + } + + @Nullable + public ItemStack getItemStack() { + return itemStack; + } + + public void setItemStack(ItemStack itemStack) { + this.itemStack = itemStack; + } + + @Nullable + public ItemMeta getItemMeta() { + return itemMeta; + } + + @Nullable + public Material getItemType() { + return itemType; + } +} diff --git a/src/main/java/io/github/sefiraat/networks/network/NetworkNode.java b/src/main/java/io/github/sefiraat/networks/network/NetworkNode.java index 5d36027b..7fd188f3 100644 --- a/src/main/java/io/github/sefiraat/networks/network/NetworkNode.java +++ b/src/main/java/io/github/sefiraat/networks/network/NetworkNode.java @@ -3,8 +3,10 @@ import io.github.sefiraat.networks.NetworkStorage; import io.github.sefiraat.networks.Networks; import me.mrCookieSlime.Slimefun.api.BlockStorage; +import org.bukkit.Color; import org.bukkit.Location; import org.bukkit.Material; +import org.bukkit.Particle; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; import org.bukkit.inventory.ItemStack; @@ -102,7 +104,17 @@ public void delete() { } public Set getChildrenNodes() { - return childrenNodes; + return this.childrenNodes; + } + + public Set getAllChildrenNodes() { + final Set nodes = new HashSet<>(); + + for (NetworkNode childrenNode : getChildrenNodes()) { + nodes.addAll(childrenNode.getAllChildrenNodes()); + } + nodes.addAll(this.childrenNodes); + return nodes; } public void addAllChildren() { diff --git a/src/main/java/io/github/sefiraat/networks/network/NetworkRoot.java b/src/main/java/io/github/sefiraat/networks/network/NetworkRoot.java index a8d59768..6d2dec4c 100644 --- a/src/main/java/io/github/sefiraat/networks/network/NetworkRoot.java +++ b/src/main/java/io/github/sefiraat/networks/network/NetworkRoot.java @@ -3,13 +3,14 @@ import io.github.mooy1.infinityexpansion.items.storage.StorageUnit; import io.github.sefiraat.networks.Networks; import io.github.sefiraat.networks.network.barrel.BarrelIdentity; -import io.github.sefiraat.networks.network.barrel.BarrelType; import io.github.sefiraat.networks.network.barrel.InfinityBarrel; import io.github.sefiraat.networks.network.barrel.NetworkShell; +import io.github.sefiraat.networks.slimefun.network.NetworkDirectional; import io.github.sefiraat.networks.slimefun.network.NetworkMemoryShell; import io.github.sefiraat.networks.slimefun.tools.CardInstance; import io.github.sefiraat.networks.slimefun.tools.NetworkCard; import io.github.sefiraat.networks.utils.Keys; +import io.github.sefiraat.networks.utils.StackUtils; import io.github.sefiraat.networks.utils.datatypes.DataTypeMethods; import io.github.sefiraat.networks.utils.datatypes.PersistentAmountInstanceType; import io.github.sefiraat.networks.utils.datatypes.PersistentCardInstanceType; @@ -39,6 +40,12 @@ public class NetworkRoot extends NetworkNode { protected final Set networkCells = new HashSet<>(); protected final Set networkExporters = new HashSet<>(); protected final Set networkImporters = new HashSet<>(); + protected final Set networkGrids = new HashSet<>(); + protected final Set networkShells = new HashSet<>(); + protected final Set networkWipers = new HashSet<>(); + protected final Set networkGrabbers = new HashSet<>(); + protected final Set networkPushers = new HashSet<>(); + protected final Set networkPurgers = new HashSet<>(); public NetworkRoot(Location location, NodeType type) { super(location, type); @@ -53,6 +60,12 @@ public void addNode(Location location, NodeType type) { case IMPORT -> networkImporters.add(location); case EXPORT -> networkExporters.add(location); case CELL -> networkCells.add(location); + case GRID -> networkGrids.add(location); + case SHELL -> networkShells.add(location); + case WIPER -> networkWipers.add(location); + case GRABBER -> networkGrabbers.add(location); + case PUSHER -> networkPushers.add(location); + case TRASH -> networkPurgers.add(location); default -> { // Not required } @@ -67,41 +80,40 @@ public Set getMonitors() { return networkMonitors; } - public Set getCells() { - return networkCells; + public Set getImports() { + return networkImporters; } public Set getExports() { return networkExporters; } - public Set getImports() { - return networkImporters; + public Set getCells() { + return networkCells; } - @Nonnull - public Map getNetworkBarrels() { - final Map menus = new HashMap<>(); - for (Location cellLocation : networkMonitors) { - for (BlockFace face : VALID_FACES) { - final Location testLocation = cellLocation.clone().add(face.getDirection()); - final SlimefunItem slimefunItem = BlockStorage.check(testLocation); + public Set getGrids() { + return networkGrids; + } - if (Networks.getSupportedPluginManager().isInfinityExpansion() - && slimefunItem instanceof StorageUnit - ) { - BlockMenu menu = BlockStorage.getInventory(testLocation); - menus.put(menu, BarrelType.INFINITY); - continue; - } + public Set getShells() { + return networkShells; + } - if (slimefunItem instanceof NetworkMemoryShell) { - BlockMenu menu = BlockStorage.getInventory(testLocation); - menus.put(menu, BarrelType.NETWORKS); - } - } - } - return menus; + public Set getWipers() { + return networkWipers; + } + + public Set getGrabbers() { + return networkGrabbers; + } + + public Set getPushers() { + return networkPushers; + } + + public Set getPurgers() { + return networkPurgers; } @Nonnull @@ -109,7 +121,7 @@ public Map getAllNetworkItems() { final Map itemStacks = new HashMap<>(); for (BarrelIdentity barrelIdentity : getBarrels()) { - final Integer currentAmount = itemStacks.get(barrelIdentity.getReferenceStack()); + final Integer currentAmount = itemStacks.get(barrelIdentity.getItemStack()); final int newAmount; if (currentAmount == null) { newAmount = barrelIdentity.getAmount(); @@ -121,7 +133,7 @@ public Map getAllNetworkItems() { newAmount = currentAmount + barrelIdentity.getAmount(); } } - itemStacks.put(barrelIdentity.getReferenceStack(), newAmount); + itemStacks.put(barrelIdentity.getItemStack(), newAmount); } for (BlockMenu blockMenu : getCellMenus()) { @@ -157,29 +169,34 @@ public Set getBarrels() { final Set barrelItemMap = new HashSet<>(); for (Location cellLocation : networkMonitors) { - for (BlockFace face : VALID_FACES) { - final Location testLocation = cellLocation.clone().add(face.getDirection()); - final SlimefunItem slimefunItem = BlockStorage.check(testLocation); + final BlockFace face = NetworkDirectional.getSelectedFace(cellLocation); - if (Networks.getSupportedPluginManager().isInfinityExpansion() - && slimefunItem instanceof StorageUnit unit - ) { - BlockMenu menu = BlockStorage.getInventory(testLocation); - InfinityBarrel infinityBarrel = getInfinityBarrel(menu, unit); - if (infinityBarrel != null) { - barrelItemMap.add(infinityBarrel); - } - continue; + if (face == null) { + continue; + } + + final Location testLocation = cellLocation.clone().add(face.getDirection()); + final SlimefunItem slimefunItem = BlockStorage.check(testLocation); + + if (Networks.getSupportedPluginManager().isInfinityExpansion() + && slimefunItem instanceof StorageUnit unit + ) { + BlockMenu menu = BlockStorage.getInventory(testLocation); + InfinityBarrel infinityBarrel = getInfinityBarrel(menu, unit); + if (infinityBarrel != null) { + barrelItemMap.add(infinityBarrel); } + continue; + } - if (slimefunItem instanceof NetworkMemoryShell memoryShell) { - BlockMenu menu = BlockStorage.getInventory(testLocation); - NetworkShell shell = getShell(menu); - if (shell != null) { - barrelItemMap.add(shell); - } + if (slimefunItem instanceof NetworkMemoryShell) { + BlockMenu menu = BlockStorage.getInventory(testLocation); + NetworkShell shell = getShell(menu); + if (shell != null) { + barrelItemMap.add(shell); } } + } return barrelItemMap; @@ -190,6 +207,11 @@ private InfinityBarrel getInfinityBarrel(@Nonnull BlockMenu blockMenu, @Nonnull final ItemStack itemStack = blockMenu.getItemInSlot(16); final Config config = BlockStorage.getLocationInfo(blockMenu.getLocation()); final String storedString = config.getString("stored"); + + if (storedString == null) { + return null; + } + final int storedInt = Integer.parseInt(storedString); if (itemStack == null || itemStack.getType() == Material.AIR) { @@ -236,7 +258,7 @@ private NetworkShell getShell(@Nonnull BlockMenu blockMenu) { final ItemStack itemStack = instance.getItemStack(); int storedInt = instance.getAmount(); - if (SlimefunUtils.isItemSimilar(output, instance.getItemStack(), true)) { + if (StackUtils.itemsMatch(instance, output)) { storedInt = storedInt + output.getAmount(); } @@ -271,37 +293,40 @@ public Set getCellMenus() { @Nullable public ItemStack getItemStack(@Nonnull ItemRequest request) { - ItemStack requestedStack = null; + ItemStack stackToReturn = null; for (BlockMenu blockMenu : getCellMenus()) { for (ItemStack itemStack : blockMenu.getContents()) { - if (itemStack != null - && itemStack.getType() != Material.AIR - && SlimefunUtils.isItemSimilar(request.getItemStack(), itemStack, true, false) + if (itemStack == null + || itemStack.getType() == Material.AIR + || !StackUtils.itemsMatch(request, itemStack) ) { - // Stack is null, so we can fill it here - if (requestedStack == null) { - requestedStack = itemStack.clone(); - requestedStack.setAmount(1); - request.receiveAmount(1); - itemStack.setAmount(itemStack.getAmount() - 1); - } + continue; + } - // Escape if fulfilled request - if (request.getAmount() <= 0) { - return requestedStack; - } + if (stackToReturn == null) { + // Return stack is null, so we can fill it here + stackToReturn = itemStack.clone(); + stackToReturn.setAmount(1); + request.receiveAmount(1); + itemStack.setAmount(itemStack.getAmount() - 1); + } - if (request.getAmount() <= itemStack.getAmount()) { - requestedStack.setAmount(requestedStack.getAmount() + request.getAmount()); - itemStack.setAmount(itemStack.getAmount() - request.getAmount()); - return requestedStack; - } else { - requestedStack.setAmount(requestedStack.getAmount() + itemStack.getAmount()); - request.receiveAmount(itemStack.getAmount()); - itemStack.setAmount(0); - } + // Escape if fulfilled request + if (request.getAmount() <= 0) { + return stackToReturn; + } + + if (request.getAmount() <= itemStack.getAmount()) { + stackToReturn.setAmount(stackToReturn.getAmount() + request.getAmount()); + itemStack.setAmount(itemStack.getAmount() - request.getAmount()); + return stackToReturn; + } else { + stackToReturn.setAmount(stackToReturn.getAmount() + itemStack.getAmount()); + request.receiveAmount(itemStack.getAmount()); + itemStack.setAmount(0); } + } } @@ -312,45 +337,46 @@ public ItemStack getItemStack(@Nonnull ItemRequest request) { if (itemStack == null || (infinity && itemStack.getAmount() == 1) - || !SlimefunUtils.isItemSimilar(request.getItemStack(), itemStack, true, false) + //|| !SlimefunUtils.isItemSimilar(request.getItemStack(), itemStack, true, false) + || !StackUtils.itemsMatch(request, itemStack) ) { continue; } // Stack is null, so we can fill it here - if (requestedStack == null) { - requestedStack = itemStack.clone(); - requestedStack.setAmount(1); + if (stackToReturn == null) { + stackToReturn = itemStack.clone(); + stackToReturn.setAmount(1); request.receiveAmount(1); itemStack.setAmount(itemStack.getAmount() - 1); } // Escape if fulfilled request if (request.getAmount() <= 0) { - return requestedStack; + return stackToReturn; } final int preserveAmount = infinity ? itemStack.getAmount() - 1 : itemStack.getAmount(); if (request.getAmount() <= preserveAmount) { - requestedStack.setAmount(requestedStack.getAmount() + request.getAmount()); + stackToReturn.setAmount(stackToReturn.getAmount() + request.getAmount()); itemStack.setAmount(itemStack.getAmount() - request.getAmount()); - return requestedStack; + return stackToReturn; } else { - requestedStack.setAmount(requestedStack.getAmount() + preserveAmount); + stackToReturn.setAmount(stackToReturn.getAmount() + preserveAmount); request.receiveAmount(preserveAmount); itemStack.setAmount(itemStack.getAmount() - preserveAmount); } } } - return requestedStack; + return stackToReturn; } public void addItemStack(@Nonnull ItemStack incomingStack) { // Run for matching barrels for (BarrelIdentity barrelIdentity : getBarrels()) { - if (SlimefunUtils.isItemSimilar(incomingStack, barrelIdentity.getReferenceStack(), true, false)) { + if (StackUtils.itemsMatch(barrelIdentity, incomingStack)) { barrelIdentity.depositItemStack(incomingStack); diff --git a/src/main/java/io/github/sefiraat/networks/network/NodeType.java b/src/main/java/io/github/sefiraat/networks/network/NodeType.java index 1e7ea5ee..102c0167 100644 --- a/src/main/java/io/github/sefiraat/networks/network/NodeType.java +++ b/src/main/java/io/github/sefiraat/networks/network/NodeType.java @@ -9,5 +9,8 @@ public enum NodeType { GRID, CELL, SHELL, - WIPER + WIPER, + GRABBER, + PUSHER, + TRASH } diff --git a/src/main/java/io/github/sefiraat/networks/network/barrel/BarrelIdentity.java b/src/main/java/io/github/sefiraat/networks/network/barrel/BarrelIdentity.java index c0a8f527..1cd61090 100644 --- a/src/main/java/io/github/sefiraat/networks/network/barrel/BarrelIdentity.java +++ b/src/main/java/io/github/sefiraat/networks/network/barrel/BarrelIdentity.java @@ -1,23 +1,26 @@ package io.github.sefiraat.networks.network.barrel; +import io.github.sefiraat.networks.network.ItemStackCache; +import io.github.sefiraat.networks.utils.StackUtils; import io.github.thebusybiscuit.slimefun4.utils.SlimefunUtils; import org.bukkit.Location; +import org.bukkit.Material; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; import javax.annotation.Nonnull; import javax.annotation.ParametersAreNonnullByDefault; -public abstract class BarrelIdentity implements BarrelCore { +public abstract class BarrelIdentity extends ItemStackCache implements BarrelCore { private final Location location; - private final ItemStack referenceStack; private final int amount; private final BarrelType type; @ParametersAreNonnullByDefault - protected BarrelIdentity(Location location, ItemStack referenceStack, int amount, BarrelType type) { + protected BarrelIdentity(Location location, ItemStack itemStack, int amount, BarrelType type) { + super(itemStack); this.location = location; - this.referenceStack = referenceStack; this.amount = amount; this.type = type; } @@ -26,10 +29,6 @@ public Location getLocation() { return this.location; } - public ItemStack getReferenceStack() { - return this.referenceStack; - } - public int getAmount() { return this.amount; } @@ -39,7 +38,7 @@ public BarrelType getType() { } public boolean holdsMatchingItem(@Nonnull ItemStack itemStack) { - return SlimefunUtils.isItemSimilar(itemStack, this.referenceStack, true, false); + return StackUtils.itemsMatch(this, itemStack); } } diff --git a/src/main/java/io/github/sefiraat/networks/slimefun/NetworkSlimefunItems.java b/src/main/java/io/github/sefiraat/networks/slimefun/NetworkSlimefunItems.java index 59ba4a7a..dea2991a 100644 --- a/src/main/java/io/github/sefiraat/networks/slimefun/NetworkSlimefunItems.java +++ b/src/main/java/io/github/sefiraat/networks/slimefun/NetworkSlimefunItems.java @@ -5,10 +5,13 @@ import io.github.sefiraat.networks.slimefun.network.NetworkCell; import io.github.sefiraat.networks.slimefun.network.NetworkController; import io.github.sefiraat.networks.slimefun.network.NetworkExport; +import io.github.sefiraat.networks.slimefun.network.NetworkGrabber; import io.github.sefiraat.networks.slimefun.network.NetworkImport; import io.github.sefiraat.networks.slimefun.network.NetworkMemoryShell; import io.github.sefiraat.networks.slimefun.network.NetworkMemoryWiper; import io.github.sefiraat.networks.slimefun.network.NetworkMonitor; +import io.github.sefiraat.networks.slimefun.network.NetworkPusher; +import io.github.sefiraat.networks.slimefun.network.NetworkPurger; import io.github.sefiraat.networks.slimefun.network.grid.NetworkCraftingGrid; import io.github.sefiraat.networks.slimefun.network.grid.NetworkGrid; import io.github.sefiraat.networks.slimefun.tools.NetworkCard; @@ -34,6 +37,9 @@ public class NetworkSlimefunItems { public static final NetworkMonitor NETWORK_MONITOR; public static final NetworkImport NETWORK_IMPORT; public static final NetworkExport NETWORK_EXPORT; + public static final NetworkGrabber NETWORK_GRABBER; + public static final NetworkPusher NETWORK_PUSHER; + public static final NetworkPurger NETWORK_TRASH; public static final NetworkGrid NETWORK_GRID; public static final NetworkCraftingGrid NETWORK_CRAFTING_GRID; public static final NetworkCell NETWORK_CELL; @@ -159,6 +165,39 @@ public class NetworkSlimefunItems { } ); + NETWORK_GRABBER = new NetworkGrabber( + NetworksItemGroups.NETWORK_ITEMS, + NetworksSlimefunItemStacks.NETWORK_GRABBER, + RecipeType.ENHANCED_CRAFTING_TABLE, + new ItemStack[]{ + OPTIC_GLASS.getItem(), OPTIC_CABLE.getItem(), OPTIC_GLASS.getItem(), + OPTIC_CABLE.getItem(), NETWORK_IMPORT.getItem(), OPTIC_CABLE.getItem(), + OPTIC_GLASS.getItem(), OPTIC_CABLE.getItem(), OPTIC_GLASS.getItem(), + } + ); + + NETWORK_PUSHER = new NetworkPusher( + NetworksItemGroups.NETWORK_ITEMS, + NetworksSlimefunItemStacks.NETWORK_PUSHER, + RecipeType.ENHANCED_CRAFTING_TABLE, + new ItemStack[]{ + OPTIC_GLASS.getItem(), OPTIC_CABLE.getItem(), OPTIC_GLASS.getItem(), + OPTIC_CABLE.getItem(), NETWORK_IMPORT.getItem(), OPTIC_CABLE.getItem(), + OPTIC_GLASS.getItem(), OPTIC_CABLE.getItem(), OPTIC_GLASS.getItem(), + } + ); + + NETWORK_TRASH = new NetworkPurger( + NetworksItemGroups.NETWORK_ITEMS, + NetworksSlimefunItemStacks.NETWORK_PURGER, + RecipeType.ENHANCED_CRAFTING_TABLE, + new ItemStack[]{ + OPTIC_GLASS.getItem(), OPTIC_CABLE.getItem(), OPTIC_GLASS.getItem(), + OPTIC_CABLE.getItem(), SlimefunItems.TRASH_CAN, OPTIC_CABLE.getItem(), + OPTIC_GLASS.getItem(), OPTIC_CABLE.getItem(), OPTIC_GLASS.getItem(), + } + ); + NETWORK_GRID = new NetworkGrid( NetworksItemGroups.NETWORK_ITEMS, NetworksSlimefunItemStacks.NETWORK_GRID, @@ -372,6 +411,9 @@ public static void setup() { NETWORK_MONITOR.register(plugin); NETWORK_IMPORT.register(plugin); NETWORK_EXPORT.register(plugin); + NETWORK_GRABBER.register(plugin); + NETWORK_PUSHER.register(plugin); + NETWORK_TRASH.register(plugin); NETWORK_GRID.register(plugin); NETWORK_CRAFTING_GRID.register(plugin); NETWORK_CELL.register(plugin); diff --git a/src/main/java/io/github/sefiraat/networks/slimefun/NetworksSlimefunItemStacks.java b/src/main/java/io/github/sefiraat/networks/slimefun/NetworksSlimefunItemStacks.java index 57cfdf0f..48d0e2e5 100644 --- a/src/main/java/io/github/sefiraat/networks/slimefun/NetworksSlimefunItemStacks.java +++ b/src/main/java/io/github/sefiraat/networks/slimefun/NetworksSlimefunItemStacks.java @@ -34,6 +34,9 @@ public class NetworksSlimefunItemStacks { public static final SlimefunItemStack NETWORK_MONITOR; public static final SlimefunItemStack NETWORK_IMPORT; public static final SlimefunItemStack NETWORK_EXPORT; + public static final SlimefunItemStack NETWORK_GRABBER; + public static final SlimefunItemStack NETWORK_PUSHER; + public static final SlimefunItemStack NETWORK_PURGER; public static final SlimefunItemStack NETWORK_GRID; public static final SlimefunItemStack NETWORK_CRAFTING_GRID; public static final SlimefunItemStack NETWORK_CELL; @@ -145,6 +148,37 @@ public class NetworksSlimefunItemStacks { "Accepts item withdrawal from cargo." ); + NETWORK_GRABBER = Theme.themedSlimefunItemStack( + "NTW_GRABBER", + new ItemStack(Material.MAGENTA_STAINED_GLASS), + Theme.MACHINE, + "Network Grabber", + "The Network Grabber will try", + "to grab the first item it finds", + "from within the selected machine." + ); + + NETWORK_PUSHER = Theme.themedSlimefunItemStack( + "NTW_PUSHER", + new ItemStack(Material.BROWN_STAINED_GLASS), + Theme.MACHINE, + "Network Pusher", + "The Network Pusher will try", + "to push a matching item from a", + "given item into the chosen machine." + ); + + NETWORK_PURGER = Theme.themedSlimefunItemStack( + "NTW_TRASH", + new ItemStack(Material.OBSERVER), + Theme.MACHINE, + "Network Purger", + "The Network Purger will pull", + "matching items from the network", + "and instantly void them.", + "Use with great care!" + ); + NETWORK_GRID = Theme.themedSlimefunItemStack( "NTW_GRID", new ItemStack(Material.NOTE_BLOCK), diff --git a/src/main/java/io/github/sefiraat/networks/slimefun/network/NetworkCell.java b/src/main/java/io/github/sefiraat/networks/slimefun/network/NetworkCell.java index 622faaf0..513d832f 100644 --- a/src/main/java/io/github/sefiraat/networks/slimefun/network/NetworkCell.java +++ b/src/main/java/io/github/sefiraat/networks/slimefun/network/NetworkCell.java @@ -14,14 +14,26 @@ import org.bukkit.inventory.ItemStack; import javax.annotation.Nonnull; +import java.util.ArrayList; +import java.util.List; public class NetworkCell extends NetworkObject { + private static final int[] SLOTS; + + static { + List integers = new ArrayList<>(); + for (int i = 0; i < 54; i++) { + integers.add(i); + } + SLOTS = integers.stream().mapToInt(Integer::intValue).toArray(); + } + public NetworkCell(ItemGroup itemGroup, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) { super(itemGroup, item, recipeType, recipe, NodeType.CELL); - for (int i = 0; i < 54; i++) { - this.getSlotsToDrop().add(i); + for (int slot : SLOTS) { + this.getSlotsToDrop().add(slot); } } @@ -42,7 +54,7 @@ public boolean canOpen(@Nonnull Block block, @Nonnull Player player) { @Override public int[] getSlotsAccessedByItemTransport(ItemTransportFlow flow) { - return new int[0]; + return new int[] {0}; } }; diff --git a/src/main/java/io/github/sefiraat/networks/slimefun/network/NetworkController.java b/src/main/java/io/github/sefiraat/networks/slimefun/network/NetworkController.java index 8445155f..9f2dcb02 100644 --- a/src/main/java/io/github/sefiraat/networks/slimefun/network/NetworkController.java +++ b/src/main/java/io/github/sefiraat/networks/slimefun/network/NetworkController.java @@ -12,6 +12,7 @@ import org.bukkit.block.Block; import org.bukkit.inventory.ItemStack; +import java.text.MessageFormat; import java.util.HashMap; import java.util.Map; diff --git a/src/main/java/io/github/sefiraat/networks/slimefun/network/NetworkDirectional.java b/src/main/java/io/github/sefiraat/networks/slimefun/network/NetworkDirectional.java new file mode 100644 index 00000000..b0fb7f1f --- /dev/null +++ b/src/main/java/io/github/sefiraat/networks/slimefun/network/NetworkDirectional.java @@ -0,0 +1,252 @@ +package io.github.sefiraat.networks.slimefun.network; + +import io.github.sefiraat.networks.network.NodeType; +import io.github.sefiraat.networks.slimefun.NetworkSlimefunItems; +import io.github.sefiraat.networks.utils.Theme; +import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup; +import io.github.thebusybiscuit.slimefun4.api.items.ItemSetting; +import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem; +import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack; +import io.github.thebusybiscuit.slimefun4.api.items.settings.IntRangeSetting; +import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType; +import io.github.thebusybiscuit.slimefun4.core.handlers.BlockPlaceHandler; +import io.github.thebusybiscuit.slimefun4.implementation.Slimefun; +import io.github.thebusybiscuit.slimefun4.libraries.dough.items.CustomItemStack; +import io.github.thebusybiscuit.slimefun4.libraries.dough.protection.Interaction; +import me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config; +import me.mrCookieSlime.Slimefun.Objects.handlers.BlockTicker; +import me.mrCookieSlime.Slimefun.api.BlockStorage; +import me.mrCookieSlime.Slimefun.api.inventory.BlockMenu; +import me.mrCookieSlime.Slimefun.api.inventory.BlockMenuPreset; +import me.mrCookieSlime.Slimefun.api.item_transport.ItemTransportFlow; +import net.md_5.bungee.api.ChatColor; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.entity.Player; +import org.bukkit.event.block.BlockPlaceEvent; +import org.bukkit.inventory.ItemFlag; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import javax.annotation.OverridingMethodsMustInvokeSuper; +import java.util.EnumSet; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +public abstract class NetworkDirectional extends NetworkObject { + + protected static final String DIRECTION = "direction"; + protected static final String UUID = "uuid"; + + private static final Set VALID_FACES = EnumSet.of( + BlockFace.UP, + BlockFace.DOWN, + BlockFace.NORTH, + BlockFace.EAST, + BlockFace.SOUTH, + BlockFace.WEST + ); + + private static final Map SELECTED_DIRECTION_MAP = new HashMap<>(); + + private final ItemSetting tickRate; + + protected NetworkDirectional(ItemGroup itemGroup, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe, NodeType type) { + super(itemGroup, item, recipeType, recipe, type); + this.tickRate = new IntRangeSetting(this, "tick_rate", 1, 1, 10); + addItemSetting(this.tickRate); + + addItemHandler( + new BlockPlaceHandler(false) { + @Override + public void onPlayerPlace(@Nonnull BlockPlaceEvent event) { + BlockStorage.addBlockInfo(event.getBlock(), UUID, event.getPlayer().getUniqueId().toString()); + BlockStorage.addBlockInfo(event.getBlock(), DIRECTION, BlockFace.SELF.name()); + } + }, + new BlockTicker() { + + private int tick = 1; + + @Override + public boolean isSynchronized() { + return false; + } + + @Override + public void tick(Block block, SlimefunItem slimefunItem, Config config) { + if (tick <= 1) { + final BlockMenu blockMenu = BlockStorage.getInventory(block); + onTick(blockMenu, block); + } + } + + @Override + public void uniqueTick() { + tick = tick <= 1 ? tickRate.getValue() : tick - 1; + } + } + ); + } + + private void updateGui(@Nullable BlockMenu blockMenu) { + if (blockMenu == null || !blockMenu.hasViewer()) { + return; + } + + BlockFace direction = getCurrentDirection(blockMenu); + + for (BlockFace blockFace : VALID_FACES) { + final SlimefunItem slimefunItem = BlockStorage.check(blockMenu.getBlock().getRelative(blockFace)); + switch (blockFace) { + case NORTH -> blockMenu.replaceExistingItem(getNorthSlot(), getDirectionalSlotPane(blockFace, slimefunItem, blockFace == direction)); + case SOUTH -> blockMenu.replaceExistingItem(getSouthSlot(), getDirectionalSlotPane(blockFace, slimefunItem, blockFace == direction)); + case EAST -> blockMenu.replaceExistingItem(getEastSlot(), getDirectionalSlotPane(blockFace, slimefunItem, blockFace == direction)); + case WEST -> blockMenu.replaceExistingItem(getWestSlot(), getDirectionalSlotPane(blockFace, slimefunItem, blockFace == direction)); + case UP -> blockMenu.replaceExistingItem(getUpSlot(), getDirectionalSlotPane(blockFace, slimefunItem, blockFace == direction)); + case DOWN -> blockMenu.replaceExistingItem(getDownSlot(), getDirectionalSlotPane(blockFace, slimefunItem, blockFace == direction)); + default -> throw new IllegalStateException("Unexpected value: " + blockFace); + } + } + } + + @Nonnull + protected BlockFace getCurrentDirection(@Nonnull BlockMenu blockMenu) { + BlockFace direction = SELECTED_DIRECTION_MAP.get(blockMenu.getLocation()); + + if (direction == null) { + direction = BlockFace.valueOf(BlockStorage.getLocationInfo(blockMenu.getLocation(), DIRECTION)); + SELECTED_DIRECTION_MAP.put(blockMenu.getLocation(), direction); + } + return direction; + } + + @OverridingMethodsMustInvokeSuper + protected void onTick(@Nullable BlockMenu blockMenu, @Nonnull Block block) { + addToRegistry(block); + updateGui(blockMenu); + } + + @Override + public void postRegister() { + new BlockMenuPreset(this.getId(), this.getItemName()) { + + @Override + public void init() { + drawBackground(getBackgroundSlots()); + + addItem(getNorthSlot(), getDirectionalSlotPane(BlockFace.NORTH, null, false), (player, i, itemStack, clickAction) -> false); + addItem(getSouthSlot(), getDirectionalSlotPane(BlockFace.SOUTH, null, false), (player, i, itemStack, clickAction) -> false); + addItem(getEastSlot(), getDirectionalSlotPane(BlockFace.EAST, null, false), (player, i, itemStack, clickAction) -> false); + addItem(getWestSlot(), getDirectionalSlotPane(BlockFace.WEST, null, false), (player, i, itemStack, clickAction) -> false); + addItem(getUpSlot(), getDirectionalSlotPane(BlockFace.UP, null, false), (player, i, itemStack, clickAction) -> false); + addItem(getDownSlot(), getDirectionalSlotPane(BlockFace.DOWN, null, false), (player, i, itemStack, clickAction) -> false); + } + + @Override + public void newInstance(@Nonnull BlockMenu blockMenu, @Nonnull Block b) { + final BlockFace direction; + final String string = BlockStorage.getLocationInfo(blockMenu.getLocation(), DIRECTION); + + if (string == null) { + // This likely means a block was placed before I made it directional + direction = BlockFace.SELF; + BlockStorage.addBlockInfo(blockMenu.getLocation(), DIRECTION, BlockFace.SELF.name()); + } else { + direction = BlockFace.valueOf(string); + } + SELECTED_DIRECTION_MAP.put(blockMenu.getLocation(), direction); + + blockMenu.addMenuClickHandler(getNorthSlot(), (player, i, itemStack, clickAction) -> setDirection(blockMenu, BlockFace.NORTH)); + blockMenu.addMenuClickHandler(getSouthSlot(), (player, i, itemStack, clickAction) -> setDirection(blockMenu, BlockFace.SOUTH)); + blockMenu.addMenuClickHandler(getEastSlot(), (player, i, itemStack, clickAction) -> setDirection(blockMenu, BlockFace.EAST)); + blockMenu.addMenuClickHandler(getWestSlot(), (player, i, itemStack, clickAction) -> setDirection(blockMenu, BlockFace.WEST)); + blockMenu.addMenuClickHandler(getUpSlot(), (player, i, itemStack, clickAction) -> setDirection(blockMenu, BlockFace.UP)); + blockMenu.addMenuClickHandler(getDownSlot(), (player, i, itemStack, clickAction) -> setDirection(blockMenu, BlockFace.DOWN)); + } + + @Override + public boolean canOpen(@Nonnull Block block, @Nonnull Player player) { + return NetworkSlimefunItems.NETWORK_GRID.canUse(player, false) + && Slimefun.getProtectionManager().hasPermission(player, block.getLocation(), Interaction.INTERACT_BLOCK); + } + + @Override + public int[] getSlotsAccessedByItemTransport(ItemTransportFlow flow) { + return new int[0]; + } + }; + } + + private boolean setDirection(@Nonnull BlockMenu blockMenu, @Nonnull BlockFace blockFace) { + SELECTED_DIRECTION_MAP.put(blockMenu.getLocation(), blockFace); + BlockStorage.addBlockInfo(blockMenu.getBlock(), DIRECTION, blockFace.name()); + return false; + } + + @Nonnull + protected int[] getBackgroundSlots() { + return new int[]{ + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 16, 17, 18, 19, 21, 23, 24, 25, 26, 27, 28, 29, 21, 32, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44 + }; + } + + protected int getNorthSlot() { + return 12; + } + + protected int getSouthSlot() { + return 30; + } + + protected int getEastSlot() { + return 22; + } + + protected int getWestSlot() { + return 20; + } + + protected int getUpSlot() { + return 15; + } + + protected int getDownSlot() { + return 33; + } + + @Nonnull + public static ItemStack getDirectionalSlotPane(@Nonnull BlockFace blockFace, @Nullable SlimefunItem slimefunItem, boolean active) { + if (slimefunItem != null) { + final ItemStack displayStack = new CustomItemStack( + slimefunItem.getItem(), + Theme.PASSIVE + "Set Direction " + blockFace.name() + " (" + ChatColor.stripColor(slimefunItem.getItemName()) + ")" + ); + final ItemMeta itemMeta = displayStack.getItemMeta(); + if (active) { + itemMeta.addEnchant(Enchantment.LUCK, 1, true); + itemMeta.addItemFlags(ItemFlag.HIDE_ENCHANTS); + } + itemMeta.setLore(null); + displayStack.setItemMeta(itemMeta); + return displayStack; + } else { + Material material = active ? Material.GREEN_STAINED_GLASS_PANE : Material.RED_STAINED_GLASS_PANE; + return new CustomItemStack( + material, + ChatColor.GRAY + "Set direction: " + blockFace.name() + ); + } + } + + @Nullable + public static BlockFace getSelectedFace(@Nonnull Location location) { + return SELECTED_DIRECTION_MAP.get(location); + } +} diff --git a/src/main/java/io/github/sefiraat/networks/slimefun/network/NetworkExport.java b/src/main/java/io/github/sefiraat/networks/slimefun/network/NetworkExport.java index 96ef787c..4fb86705 100644 --- a/src/main/java/io/github/sefiraat/networks/slimefun/network/NetworkExport.java +++ b/src/main/java/io/github/sefiraat/networks/slimefun/network/NetworkExport.java @@ -109,7 +109,6 @@ private void tryFetchItem(@Nonnull BlockMenu blockMenu) { } ItemStack clone = testItem.clone(); - clone.setAmount(1); ItemRequest itemRequest = new ItemRequest(clone, clone.getMaxStackSize()); ItemStack retrieved = definition.getNode().getRoot().getItemStack(itemRequest); diff --git a/src/main/java/io/github/sefiraat/networks/slimefun/network/NetworkGrabber.java b/src/main/java/io/github/sefiraat/networks/slimefun/network/NetworkGrabber.java new file mode 100644 index 00000000..0e9b2815 --- /dev/null +++ b/src/main/java/io/github/sefiraat/networks/slimefun/network/NetworkGrabber.java @@ -0,0 +1,64 @@ +package io.github.sefiraat.networks.slimefun.network; + +import io.github.sefiraat.networks.NetworkStorage; +import io.github.sefiraat.networks.network.NodeDefinition; +import io.github.sefiraat.networks.network.NodeType; +import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup; +import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack; +import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType; +import me.mrCookieSlime.Slimefun.api.BlockStorage; +import me.mrCookieSlime.Slimefun.api.inventory.BlockMenu; +import me.mrCookieSlime.Slimefun.api.item_transport.ItemTransportFlow; +import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; +import org.bukkit.inventory.ItemStack; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +public class NetworkGrabber extends NetworkDirectional { + + + public NetworkGrabber(ItemGroup itemGroup, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) { + super(itemGroup, item, recipeType, recipe, NodeType.GRABBER); + } + + @Override + protected void onTick(@Nullable BlockMenu blockMenu, @Nonnull Block block) { + super.onTick(blockMenu, block); + if (blockMenu != null) { + tryGrabItem(blockMenu); + } + } + + private void tryGrabItem(@Nonnull BlockMenu blockMenu) { + final NodeDefinition definition = NetworkStorage.getAllNetworkObjects().get(blockMenu.getLocation()); + + if (definition.getNode() == null) { + return; + } + + final BlockFace direction = this.getCurrentDirection(blockMenu); + final BlockMenu targetMenu = BlockStorage.getInventory(blockMenu.getBlock().getRelative(direction)); + + if (targetMenu == null) { + return; + } + + int[] slots = targetMenu.getPreset().getSlotsAccessedByItemTransport(targetMenu, ItemTransportFlow.WITHDRAW, null); + + if (slots.length == 0) { + slots = targetMenu.getPreset().getSlotsAccessedByItemTransport(ItemTransportFlow.WITHDRAW); + } + + for (int slot : slots) { + final ItemStack itemStack = targetMenu.getItemInSlot(slot); + + if (itemStack != null && itemStack.getType() != Material.AIR) { + definition.getNode().getRoot().addItemStack(itemStack); + break; + } + } + } +} diff --git a/src/main/java/io/github/sefiraat/networks/slimefun/network/NetworkMemoryShell.java b/src/main/java/io/github/sefiraat/networks/slimefun/network/NetworkMemoryShell.java index e4d094f1..133de9a1 100644 --- a/src/main/java/io/github/sefiraat/networks/slimefun/network/NetworkMemoryShell.java +++ b/src/main/java/io/github/sefiraat/networks/slimefun/network/NetworkMemoryShell.java @@ -233,11 +233,13 @@ private static CardInstance getAmountInstance(@Nonnull ItemStack card) { } private static boolean itemMatch(@Nonnull ItemStack itemStack, @Nonnull CardInstance instance) { - if (itemStack.getType() != instance.getType()) { + if (itemStack.getType() != instance.getItemType()) { return false; } if (itemStack.hasItemMeta()) { - return instance.getItemMeta() != null && itemStack.getItemMeta().equals(instance.getItemMeta()); + final ItemMeta itemMeta = itemStack.getItemMeta(); + final ItemMeta cachedMeta = instance.getItemMeta(); + return itemMeta.equals(cachedMeta); } else { return instance.getItemMeta() == null; } @@ -274,6 +276,11 @@ public boolean canOpen(@Nonnull Block block, @Nonnull Player player) { @Override public int[] getSlotsAccessedByItemTransport(ItemTransportFlow flow) { + if (flow == ItemTransportFlow.INSERT) { + return new int[] {INPUT_SLOT}; + } else if (flow == ItemTransportFlow.WITHDRAW) { + return new int[] {OUTPUT_SLOT}; + } return new int[0]; } diff --git a/src/main/java/io/github/sefiraat/networks/slimefun/network/NetworkMonitor.java b/src/main/java/io/github/sefiraat/networks/slimefun/network/NetworkMonitor.java index e2c88ba0..39c23a74 100644 --- a/src/main/java/io/github/sefiraat/networks/slimefun/network/NetworkMonitor.java +++ b/src/main/java/io/github/sefiraat/networks/slimefun/network/NetworkMonitor.java @@ -6,7 +6,7 @@ import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType; import org.bukkit.inventory.ItemStack; -public class NetworkMonitor extends NetworkObject { +public class NetworkMonitor extends NetworkDirectional { public NetworkMonitor(ItemGroup itemGroup, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) { super(itemGroup, item, recipeType, recipe, NodeType.STORAGE_MONITOR); diff --git a/src/main/java/io/github/sefiraat/networks/slimefun/network/NetworkObject.java b/src/main/java/io/github/sefiraat/networks/slimefun/network/NetworkObject.java index aefaca1e..88b304d2 100644 --- a/src/main/java/io/github/sefiraat/networks/slimefun/network/NetworkObject.java +++ b/src/main/java/io/github/sefiraat/networks/slimefun/network/NetworkObject.java @@ -13,6 +13,7 @@ import me.mrCookieSlime.Slimefun.Objects.handlers.BlockTicker; import me.mrCookieSlime.Slimefun.api.BlockStorage; import me.mrCookieSlime.Slimefun.api.inventory.BlockMenu; +import org.bukkit.Location; import org.bukkit.block.Block; import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.inventory.ItemStack; @@ -62,11 +63,15 @@ public void addToRegistry(@Nonnull Block block) { } public void onBreak(@Nonnull BlockBreakEvent event) { - BlockMenu blockMenu = BlockStorage.getInventory(event.getBlock()); - for (Integer i : this.slotsToDrop) { - blockMenu.dropItems(blockMenu.getLocation(), i); + final Location location = event.getBlock().getLocation(); + final BlockMenu blockMenu = BlockStorage.getInventory(event.getBlock()); + + if (blockMenu != null) { + for (int i : this.slotsToDrop) { + blockMenu.dropItems(location, i); + } } - NetworkStorage.getAllNetworkObjects().remove(event.getBlock().getLocation()); - BlockStorage.clearBlockInfo(event.getBlock().getLocation()); + NetworkStorage.removeNode(location); + BlockStorage.clearBlockInfo(location); } } diff --git a/src/main/java/io/github/sefiraat/networks/slimefun/network/NetworkPurger.java b/src/main/java/io/github/sefiraat/networks/slimefun/network/NetworkPurger.java new file mode 100644 index 00000000..99706335 --- /dev/null +++ b/src/main/java/io/github/sefiraat/networks/slimefun/network/NetworkPurger.java @@ -0,0 +1,137 @@ +package io.github.sefiraat.networks.slimefun.network; + +import io.github.sefiraat.networks.NetworkStorage; +import io.github.sefiraat.networks.network.ItemRequest; +import io.github.sefiraat.networks.network.NodeDefinition; +import io.github.sefiraat.networks.network.NodeType; +import io.github.sefiraat.networks.slimefun.NetworkSlimefunItems; +import io.github.sefiraat.networks.utils.Theme; +import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup; +import io.github.thebusybiscuit.slimefun4.api.items.ItemSetting; +import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem; +import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack; +import io.github.thebusybiscuit.slimefun4.api.items.settings.IntRangeSetting; +import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType; +import io.github.thebusybiscuit.slimefun4.core.handlers.BlockBreakHandler; +import io.github.thebusybiscuit.slimefun4.implementation.Slimefun; +import io.github.thebusybiscuit.slimefun4.libraries.dough.items.CustomItemStack; +import io.github.thebusybiscuit.slimefun4.libraries.dough.protection.Interaction; +import me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config; +import me.mrCookieSlime.Slimefun.Objects.handlers.BlockTicker; +import me.mrCookieSlime.Slimefun.api.BlockStorage; +import me.mrCookieSlime.Slimefun.api.inventory.BlockMenu; +import me.mrCookieSlime.Slimefun.api.inventory.BlockMenuPreset; +import me.mrCookieSlime.Slimefun.api.item_transport.ItemTransportFlow; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.Particle; +import org.bukkit.block.Block; +import org.bukkit.entity.Player; +import org.bukkit.event.block.BlockBreakEvent; +import org.bukkit.inventory.ItemStack; + +import javax.annotation.Nonnull; +import java.util.List; + +public class NetworkPurger extends NetworkObject { + + private static final int[] BACKGROUND_SLOTS = {0, 1, 2, 6, 7, 8, 9, 10, 11, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26}; + private static final int TEST_ITEM_SLOT = 13; + private static final int[] TEST_ITEM_BACKDROP = {3, 4, 5, 12, 14, 21, 22, 23}; + + private static final CustomItemStack TEST_BACKDROP_STACK = new CustomItemStack( + Material.GREEN_STAINED_GLASS_PANE, + Theme.SUCCESS + "Purge Item Matching" + ); + + private final ItemSetting tickRate; + + public NetworkPurger(ItemGroup itemGroup, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) { + super(itemGroup, item, recipeType, recipe, NodeType.TRASH); + this.tickRate = new IntRangeSetting(this, "tick_rate", 1, 1, 10); + addItemSetting(this.tickRate); + + this.getSlotsToDrop().add(TEST_ITEM_SLOT); + + addItemHandler( + new BlockTicker() { + + private int tick = 1; + + @Override + public boolean isSynchronized() { + return false; + } + + @Override + public void tick(Block block, SlimefunItem item, Config data) { + if (tick <= 1) { + final BlockMenu blockMenu = BlockStorage.getInventory(block); + addToRegistry(block); + tryKillItem(blockMenu); + } + } + + @Override + public void uniqueTick() { + tick = tick <= 1 ? tickRate.getValue() : tick - 1; + } + }, + new BlockBreakHandler(true, true) { + @Override + public void onPlayerBreak(BlockBreakEvent e, ItemStack item, List drops) { + BlockMenu blockMenu = BlockStorage.getInventory(e.getBlock()); + blockMenu.dropItems(blockMenu.getLocation(), TEST_ITEM_SLOT); + } + } + ); + } + + private void tryKillItem(@Nonnull BlockMenu blockMenu) { + final NodeDefinition definition = NetworkStorage.getAllNetworkObjects().get(blockMenu.getLocation()); + + if (definition.getNode() == null) { + return; + } + + ItemStack testItem = blockMenu.getItemInSlot(TEST_ITEM_SLOT); + + if (testItem == null) { + return; + } + + ItemStack clone = testItem.clone(); + clone.setAmount(1); + + ItemRequest itemRequest = new ItemRequest(clone, clone.getMaxStackSize()); + ItemStack retrieved = definition.getNode().getRoot().getItemStack(itemRequest); + if (retrieved != null) { + retrieved.setAmount(0); + Location location = blockMenu.getLocation().clone().add(0.5, 1.2, 0.5); + location.getWorld().spawnParticle(Particle.SMOKE_NORMAL, location, 1, 0.5, 0, 0.2, 0); + } + } + + @Override + public void postRegister() { + new BlockMenuPreset(this.getId(), this.getItemName()) { + + @Override + public void init() { + drawBackground(BACKGROUND_SLOTS); + drawBackground(TEST_BACKDROP_STACK, TEST_ITEM_BACKDROP); + } + + @Override + public boolean canOpen(@Nonnull Block block, @Nonnull Player player) { + return NetworkSlimefunItems.NETWORK_GRID.canUse(player, false) + && Slimefun.getProtectionManager().hasPermission(player, block.getLocation(), Interaction.INTERACT_BLOCK); + } + + @Override + public int[] getSlotsAccessedByItemTransport(ItemTransportFlow flow) { + return new int[0]; + } + }; + } +} diff --git a/src/main/java/io/github/sefiraat/networks/slimefun/network/NetworkPusher.java b/src/main/java/io/github/sefiraat/networks/slimefun/network/NetworkPusher.java new file mode 100644 index 00000000..d78abf83 --- /dev/null +++ b/src/main/java/io/github/sefiraat/networks/slimefun/network/NetworkPusher.java @@ -0,0 +1,116 @@ +package io.github.sefiraat.networks.slimefun.network; + +import io.github.sefiraat.networks.NetworkStorage; +import io.github.sefiraat.networks.network.ItemRequest; +import io.github.sefiraat.networks.network.NodeDefinition; +import io.github.sefiraat.networks.network.NodeType; +import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup; +import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack; +import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType; +import me.mrCookieSlime.Slimefun.api.BlockStorage; +import me.mrCookieSlime.Slimefun.api.inventory.BlockMenu; +import me.mrCookieSlime.Slimefun.api.item_transport.ItemTransportFlow; +import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.inventory.ItemStack; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +public class NetworkPusher extends NetworkDirectional { + + public static final int TEMPLATE_SLOT = 25; + + public NetworkPusher(ItemGroup itemGroup, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) { + super(itemGroup, item, recipeType, recipe, NodeType.PUSHER); + this.getSlotsToDrop().add(TEMPLATE_SLOT); + } + + @Override + protected void onTick(@Nullable BlockMenu blockMenu, @Nonnull Block block) { + super.onTick(blockMenu, block); + if (blockMenu != null) { + tryPushItem(blockMenu); + } + } + + private void tryPushItem(@Nonnull BlockMenu blockMenu) { + final NodeDefinition definition = NetworkStorage.getAllNetworkObjects().get(blockMenu.getLocation()); + + if (definition.getNode() == null) { + return; + } + + ItemStack testItem = blockMenu.getItemInSlot(TEMPLATE_SLOT); + + if (testItem == null || testItem.getType() == Material.AIR) { + return; + } + + final BlockMenu targetMenu = BlockStorage.getInventory(blockMenu.getBlock().getRelative(getCurrentDirection(blockMenu))); + + if (targetMenu == null) { + return; + } + + final ItemStack clone = testItem.clone(); + clone.setAmount(1); + final ItemRequest itemRequest = new ItemRequest(clone, clone.getMaxStackSize()); + + int[] slots = targetMenu.getPreset().getSlotsAccessedByItemTransport(blockMenu, ItemTransportFlow.INSERT, null); + + if (slots.length == 0) { + slots = targetMenu.getPreset().getSlotsAccessedByItemTransport(ItemTransportFlow.INSERT); + } + + for (int slot : slots) { + final ItemStack itemStack = targetMenu.getItemInSlot(slot); + + if (itemStack == null || itemStack.getType() != Material.AIR) { + ItemStack retrieved = definition.getNode().getRoot().getItemStack(itemRequest); + if (retrieved != null) { + targetMenu.pushItem(retrieved, slots); + } + break; + } + } + } + + @Nonnull + @Override + protected int[] getBackgroundSlots() { + return new int[]{ + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 15, 16, 17, 18, 20, 22, 23, 24, 26, 27, 28, 30, 31, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44 + }; + } + + @Override + protected int getNorthSlot() { + return 11; + } + + @Override + protected int getSouthSlot() { + return 29; + } + + @Override + protected int getEastSlot() { + return 21; + } + + @Override + protected int getWestSlot() { + return 19; + } + + @Override + protected int getUpSlot() { + return 14; + } + + @Override + protected int getDownSlot() { + return 32; + } +} diff --git a/src/main/java/io/github/sefiraat/networks/slimefun/network/grid/NetworkGrid.java b/src/main/java/io/github/sefiraat/networks/slimefun/network/grid/NetworkGrid.java index 1e9c925e..5b3e70af 100644 --- a/src/main/java/io/github/sefiraat/networks/slimefun/network/grid/NetworkGrid.java +++ b/src/main/java/io/github/sefiraat/networks/slimefun/network/grid/NetworkGrid.java @@ -7,6 +7,7 @@ import io.github.sefiraat.networks.network.NodeType; import io.github.sefiraat.networks.slimefun.NetworkSlimefunItems; import io.github.sefiraat.networks.slimefun.network.NetworkObject; +import io.github.sefiraat.networks.utils.StackUtils; import io.github.sefiraat.networks.utils.Theme; import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup; import io.github.thebusybiscuit.slimefun4.api.items.ItemSetting; @@ -16,6 +17,7 @@ import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType; import io.github.thebusybiscuit.slimefun4.implementation.Slimefun; import io.github.thebusybiscuit.slimefun4.libraries.dough.items.CustomItemStack; +import io.github.thebusybiscuit.slimefun4.libraries.dough.protection.ActionType; import io.github.thebusybiscuit.slimefun4.libraries.dough.protection.Interaction; import me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config; import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ClickAction; @@ -241,17 +243,31 @@ boolean retrieveItem(Player player, NodeDefinition definition, ItemStack itemSta } final GridItemRequest request = new GridItemRequest(clone, amount, player); + final ItemStack cursor = player.getItemOnCursor(); - // Process item request - if (player.getItemOnCursor().getType() == Material.AIR) { - ItemStack requestingStack = definition.getNode().getRoot().getItemStack(request); - if (requestingStack != null) { - request.getPlayer().setItemOnCursor(requestingStack); + // Quickly check if the cursor has an item and if we can add more to it + if (cursor.getType() != Material.AIR && !canAddMore(action, cursor, request)) { + return false; + } + + final ItemStack requestingStack = definition.getNode().getRoot().getItemStack(request); + if (requestingStack != null) { + if (cursor.getType() != Material.AIR) { + requestingStack.setAmount(cursor.getAmount() + 1); } + request.getPlayer().setItemOnCursor(requestingStack); } + return false; } + private boolean canAddMore(@Nonnull ClickAction action, @Nonnull ItemStack cursor, @Nonnull GridItemRequest request) { + return !action.isRightClicked() + && request.getAmount() == 1 + && cursor.getAmount() < cursor.getMaxStackSize() + && StackUtils.itemsMatch(request, cursor); + } + @Override public void postRegister() { new BlockMenuPreset(this.getId(), this.getItemName()) { diff --git a/src/main/java/io/github/sefiraat/networks/slimefun/tools/CardInstance.java b/src/main/java/io/github/sefiraat/networks/slimefun/tools/CardInstance.java index d36c02e1..2ecbcfaa 100644 --- a/src/main/java/io/github/sefiraat/networks/slimefun/tools/CardInstance.java +++ b/src/main/java/io/github/sefiraat/networks/slimefun/tools/CardInstance.java @@ -1,50 +1,27 @@ package io.github.sefiraat.networks.slimefun.tools; +import io.github.sefiraat.networks.network.ItemStackCache; import io.github.sefiraat.networks.utils.Theme; import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; +import javax.annotation.Nonnull; import javax.annotation.Nullable; import java.util.List; -public class CardInstance { +public class CardInstance extends ItemStackCache { - @Nullable - private ItemStack itemStack; - @Nullable - private Material type; - @Nullable - private ItemMeta itemMeta; private int amount; private final int limit; public CardInstance(@Nullable ItemStack itemStack, int amount, int limit) { - this.itemStack = itemStack; - if (this.itemStack != null) { - this.itemMeta = itemStack.hasItemMeta() ? itemStack.getItemMeta() : null; - this.type = itemStack.getType(); - } + super(itemStack); this.amount = amount; this.limit = limit; } - @Nullable - public ItemStack getItemStack() { - return this.itemStack; - } - - @Nullable - public Material getType() { - return type; - } - - @Nullable - public ItemMeta getItemMeta() { - return itemMeta; - } - public int getAmount() { return this.amount; } @@ -53,24 +30,16 @@ public int getLimit() { return this.limit; } - public void setItemStack(@Nullable ItemStack itemStack) { - this.itemStack = itemStack; - if (this.itemStack != null) { - this.itemMeta = itemStack.hasItemMeta() ? itemStack.getItemMeta() : null; - this.type = itemStack.getType(); - } - } - public void setAmount(int amount) { this.amount = amount; } @Nullable public ItemStack withdrawStack(int amount) { - if (this.itemStack == null) { + if (this.getItemStack() == null) { return null; } - final ItemStack clone = this.itemStack.clone(); + final ItemStack clone = this.getItemStack().clone(); clone.setAmount(Math.min(this.amount, amount)); reduceAmount(clone.getAmount()); return clone; @@ -78,10 +47,10 @@ public ItemStack withdrawStack(int amount) { @Nullable public ItemStack withdrawStack() { - if (this.itemStack == null) { + if (this.getItemStack() == null) { return null; } - return withdrawStack(this.itemStack.getMaxStackSize()); + return withdrawStack(this.getItemStack().getMaxStackSize()); } public void increaseAmount(int amount) { @@ -97,18 +66,18 @@ public void reduceAmount(int amount) { this.amount = this.amount - amount; } - public void updateLore(ItemMeta itemMeta) { + public void updateLore(@Nonnull ItemMeta itemMeta) { List lore = itemMeta.getLore(); lore.set(10, getLoreLine()); itemMeta.setLore(lore); } public String getLoreLine() { - if (this.itemStack == null) { + if (this.getItemStack() == null) { return Theme.WARNING + "Empty"; } - ItemMeta itemMeta = this.itemStack.getItemMeta(); - String name = itemMeta.hasDisplayName() ? ChatColor.stripColor(itemMeta.getDisplayName()) : this.itemStack.getType().name(); + ItemMeta itemMeta = this.getItemMeta(); + String name = itemMeta.hasDisplayName() ? ChatColor.stripColor(itemMeta.getDisplayName()) : this.getItemType().name(); return Theme.CLICK_INFO + name + ": " + Theme.PASSIVE + this.amount; } } diff --git a/src/main/java/io/github/sefiraat/networks/slimefun/tools/NetworkProbe.java b/src/main/java/io/github/sefiraat/networks/slimefun/tools/NetworkProbe.java index 67e096a4..8f5974b9 100644 --- a/src/main/java/io/github/sefiraat/networks/slimefun/tools/NetworkProbe.java +++ b/src/main/java/io/github/sefiraat/networks/slimefun/tools/NetworkProbe.java @@ -3,6 +3,7 @@ import io.github.sefiraat.networks.network.NetworkRoot; import io.github.sefiraat.networks.slimefun.network.NetworkController; import io.github.sefiraat.networks.utils.GeneralUtils; +import io.github.sefiraat.networks.utils.Theme; import io.github.thebusybiscuit.slimefun4.api.events.PlayerRightClickEvent; import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup; import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem; @@ -15,6 +16,7 @@ import javax.annotation.Nonnull; import java.text.MessageFormat; +import java.util.Map; import java.util.Optional; public class NetworkProbe extends SlimefunItem { @@ -32,7 +34,7 @@ public void onRightClick(PlayerRightClickEvent e) { SlimefunItem slimefunItem = SlimefunItem.getByItem(player.getInventory().getItemInMainHand()); if (slimefunItem instanceof NetworkProbe) { displayToPlayer(block, player); - GeneralUtils.putOnCooldown(e.getItem(), 5); + GeneralUtils.putOnCooldown(e.getItem(), 10); e.cancel(); } } @@ -44,38 +46,36 @@ public void onRightClick(PlayerRightClickEvent e) { private void displayToPlayer(@Nonnull Block block, @Nonnull Player player) { NetworkRoot root = NetworkController.NETWORKS.get(block.getLocation()); if (root != null) { + final int bridges = root.getBridges().size(); + final int importers = root.getImports().size(); + final int exporters = root.getExports().size(); + final int monitors = root.getMonitors().size(); + final int shells = root.getShells().size(); + final int cells = root.getCells().size(); + final int wipers = root.getWipers().size(); + final int grabbers = root.getGrabbers().size(); + final int pushers = root.getPushers().size(); + final int purgers = root.getPushers().size(); - long time = System.nanoTime(); + final Map allNetworkItems = root.getAllNetworkItems(); + final int distinctItems = allNetworkItems.size(); + long totalItems = allNetworkItems.values().stream().mapToLong(integer -> integer).sum(); - int bridges = root.getBridges().size(); - long timeBridge = System.nanoTime(); - - int importers = root.getImports().size(); - long timeImporter = System.nanoTime(); - - int exporters = root.getExports().size(); - long timeExporter = System.nanoTime(); - - int monitors = root.getMonitors().size(); - long timeMonitors = System.nanoTime(); - - int cells = root.getCells().size(); - long timeCells = System.nanoTime(); - - int barrels = root.getNetworkBarrels().size(); - long timeBarrels = System.nanoTime(); - - int items = root.getAllNetworkItems().size(); - long timeItems = System.nanoTime(); - - player.sendMessage(""); - player.sendMessage(MessageFormat.format("Bridges: {0} (Time taken: {1} ns)", bridges, timeBridge - time)); - player.sendMessage(MessageFormat.format("Importers: {0} (Time taken: {1} ns)", importers, timeImporter - timeBridge)); - player.sendMessage(MessageFormat.format("Exporters: {0} (Time taken: {1} ns)", exporters, timeExporter - timeImporter)); - player.sendMessage(MessageFormat.format("Monitors: {0} (Time taken: {1} ns)", monitors, timeMonitors - timeExporter)); - player.sendMessage(MessageFormat.format("Cells: {0} (Time taken: {1} ns)", cells, timeCells - timeMonitors)); - player.sendMessage(MessageFormat.format("Barrels: {0} (Time taken: {1} ns)", barrels, timeBarrels - timeCells)); - player.sendMessage(MessageFormat.format("Items: {0} (Time taken: {1} ns)", items, timeItems - timeBarrels)); + player.sendMessage("------------------------------"); + player.sendMessage(" Network Summary "); + player.sendMessage("------------------------------"); + player.sendMessage(MessageFormat.format("{1}Bridges: {2}{0}", bridges, Theme.CLICK_INFO, Theme.PASSIVE)); + player.sendMessage(MessageFormat.format("{1}Importers: {2}{0}", importers, Theme.CLICK_INFO, Theme.PASSIVE)); + player.sendMessage(MessageFormat.format("{1}Exporters: {2}{0}", exporters, Theme.CLICK_INFO, Theme.PASSIVE)); + player.sendMessage(MessageFormat.format("{1}Monitors: {2}{0}", monitors, Theme.CLICK_INFO, Theme.PASSIVE)); + player.sendMessage(MessageFormat.format("{1}Shells: {2}{0}", shells, Theme.CLICK_INFO, Theme.PASSIVE)); + player.sendMessage(MessageFormat.format("{1}Cells: {2}{0}", cells, Theme.CLICK_INFO, Theme.PASSIVE)); + player.sendMessage(MessageFormat.format("{1}Wipers: {2}{0}", wipers, Theme.CLICK_INFO, Theme.PASSIVE)); + player.sendMessage(MessageFormat.format("{1}Grabbers: {2}{0}", grabbers, Theme.CLICK_INFO, Theme.PASSIVE)); + player.sendMessage(MessageFormat.format("{1}Pushers: {2}{0}", pushers, Theme.CLICK_INFO, Theme.PASSIVE)); + player.sendMessage(MessageFormat.format("{1}Purgers: {2}{0}", purgers, Theme.CLICK_INFO, Theme.PASSIVE)); + player.sendMessage(MessageFormat.format("{1}Distinct Items: {2}{0}", distinctItems, Theme.CLICK_INFO, Theme.PASSIVE)); + player.sendMessage(MessageFormat.format("{1}Total Items: {2}{0}", totalItems, Theme.CLICK_INFO, Theme.PASSIVE)); } } diff --git a/src/main/java/io/github/sefiraat/networks/utils/StackUtils.java b/src/main/java/io/github/sefiraat/networks/utils/StackUtils.java index a07c8535..ec852034 100644 --- a/src/main/java/io/github/sefiraat/networks/utils/StackUtils.java +++ b/src/main/java/io/github/sefiraat/networks/utils/StackUtils.java @@ -1,7 +1,9 @@ package io.github.sefiraat.networks.utils; +import io.github.sefiraat.networks.network.ItemStackCache; import lombok.experimental.UtilityClass; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; import javax.annotation.Nonnull; @@ -15,4 +17,17 @@ public static ItemStack getAsQuantity(@Nonnull ItemStack itemStack, int amount) return clone; } + public static boolean itemsMatch(@Nonnull ItemStackCache cache, @Nonnull ItemStack itemStack) { + if (itemStack.getType() != cache.getItemType()) { + return false; + } + if (itemStack.hasItemMeta()) { + final ItemMeta itemMeta = itemStack.getItemMeta(); + final ItemMeta cachedMeta = cache.getItemMeta(); + return itemMeta.equals(cachedMeta); + } else { + return cache.getItemMeta() == null; + } + } + }