From 19c7e235edd11d97ae995aa96111834fbe771d3a Mon Sep 17 00:00:00 2001 From: TheBusyBiscuit Date: Sun, 19 Sep 2021 19:39:06 +0200 Subject: [PATCH] Small improvements --- .../core/guide/SlimefunGuideMode.java | 4 +- .../core/services/CustomItemDataService.java | 41 +++++++++++++++++-- .../slimefun4/utils/SlimefunUtils.java | 26 +++++++----- 3 files changed, 54 insertions(+), 17 deletions(-) diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/guide/SlimefunGuideMode.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/guide/SlimefunGuideMode.java index 25c6776b4..5b269f96e 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/guide/SlimefunGuideMode.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/guide/SlimefunGuideMode.java @@ -2,8 +2,6 @@ package io.github.thebusybiscuit.slimefun4.core.guide; import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem; -import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ChestMenu; - /** * This enum holds the different designs a {@link SlimefunGuide} can have. * Each constant corresponds to a {@link SlimefunGuideImplementation}. @@ -17,7 +15,7 @@ import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ChestMenu; public enum SlimefunGuideMode { /** - * This design is the standard layout, it uses a {@link ChestMenu} + * This design is the standard layout used in survival mode. */ SURVIVAL_MODE, diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/CustomItemDataService.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/CustomItemDataService.java index fa5d25862..e9617b7ee 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/CustomItemDataService.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/CustomItemDataService.java @@ -99,8 +99,7 @@ public class CustomItemDataService implements Keyed { * * @return An {@link Optional} describing the result */ - @Nonnull - public Optional getItemData(@Nullable ItemStack item) { + public @Nonnull Optional getItemData(@Nullable ItemStack item) { if (item == null || item.getType() == Material.AIR || !item.hasItemMeta()) { return Optional.empty(); } @@ -117,12 +116,46 @@ public class CustomItemDataService implements Keyed { * * @return An {@link Optional} describing the result */ - @Nonnull - public Optional getItemData(@Nonnull ItemMeta meta) { + public @Nonnull Optional getItemData(@Nonnull ItemMeta meta) { Validate.notNull(meta, "Cannot read data from null!"); PersistentDataContainer container = meta.getPersistentDataContainer(); return Optional.ofNullable(container.get(namespacedKey, PersistentDataType.STRING)); } + /** + * This method compares the custom data stored on two {@link ItemMeta} objects. + * This method will only return {@literal true} if both {@link ItemMeta}s contain + * custom data and if both of their data values are equal. + * + * @param meta1 + * The first {@link ItemMeta} + * @param meta2 + * The second {@link ItemMeta} + * + * @return Whether both metas have data on them and its the same. + */ + public boolean hasEqualItemData(@Nonnull ItemMeta meta1, @Nonnull ItemMeta meta2) { + Validate.notNull(meta1, "Cannot read data from null (first arg)"); + Validate.notNull(meta2, "Cannot read data from null (second arg)"); + + Optional data1 = getItemData(meta1); + + // Check if the first data is present + if (data1.isPresent()) { + // Only retrieve the second data where necessary. + Optional data2 = getItemData(meta2); + + /* + * Check if both are present and equal. + * Optional#equals(...) compares their values, so no need + * to call Optional#get() here. + */ + return data2.isPresent() && data1.equals(data2); + } else { + // No value present, we can return immediately. + return false; + } + } + } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/utils/SlimefunUtils.java b/src/main/java/io/github/thebusybiscuit/slimefun4/utils/SlimefunUtils.java index 8c9c26ee2..64b00c3bd 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/utils/SlimefunUtils.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/utils/SlimefunUtils.java @@ -288,18 +288,20 @@ public final class SlimefunUtils { ItemMetaSnapshot meta = ((SlimefunItemStack) sfitem).getItemMetaSnapshot(); return equalsItemMeta(itemMeta, meta, checkLore); } else if (sfitem instanceof ItemStackWrapper && sfitem.hasItemMeta()) { - // Slimefun items may be ItemStackWrapper's in the context of cargo so let's try to do - // an ID comparison before meta comparison + /* + * Cargo optimization (PR #3258) + * + * Slimefun items may be ItemStackWrapper's in the context of cargo + * so let's try to do an ID comparison before meta comparison + */ ItemMeta possibleSfItemMeta = sfitem.getItemMeta(); - Optional possibleSfItemId = Slimefun.getItemDataService().getItemData(possibleSfItemMeta); - Optional itemId = Slimefun.getItemDataService().getItemData(itemMeta); - if (possibleSfItemId.isPresent() && itemId.isPresent()) { - return possibleSfItemId.get().equals(itemId.get()); + // Prioritize SlimefunItem id comparison over ItemMeta comparison + if (Slimefun.getItemDataService().hasEqualItemData(possibleSfItemMeta, itemMeta)) { + return true; } else { return equalsItemMeta(itemMeta, possibleSfItemMeta, checkLore); } - } else if (sfitem.hasItemMeta()) { return equalsItemMeta(itemMeta, sfitem.getItemMeta(), checkLore); } else { @@ -338,8 +340,11 @@ public final class SlimefunUtils { } else if (checkLore) { boolean hasItemMetaLore = itemMeta.hasLore(); boolean hasSfItemMetaLore = sfitemMeta.hasLore(); - if (hasItemMetaLore && hasSfItemMetaLore && !equalsLore(itemMeta.getLore(), sfitemMeta.getLore())) { - return false; + + if (hasItemMetaLore && hasSfItemMetaLore) { + if (!equalsLore(itemMeta.getLore(), sfitemMeta.getLore())) { + return false; + } } else if (hasItemMetaLore != hasSfItemMetaLore) { return false; } @@ -493,7 +498,8 @@ public final class SlimefunUtils { * Helper method to check if an Inventory is empty (has no items in "storage"). If the MC version is 1.16 or above * this will call {@link Inventory#isEmpty()} (Which calls MC code resulting in a faster method). * - * @param inventory The {@link Inventory} to check. + * @param inventory + * The {@link Inventory} to check. * @return True if the inventory is empty and false otherwise */ public static boolean isInventoryEmpty(@Nonnull Inventory inventory) {