1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-19 19:25:48 +00:00

Small improvements

This commit is contained in:
TheBusyBiscuit 2021-09-19 19:39:06 +02:00
parent 4727b2a5dd
commit 19c7e235ed
3 changed files with 54 additions and 17 deletions

View File

@ -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,

View File

@ -99,8 +99,7 @@ public class CustomItemDataService implements Keyed {
*
* @return An {@link Optional} describing the result
*/
@Nonnull
public Optional<String> getItemData(@Nullable ItemStack item) {
public @Nonnull Optional<String> 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<String> getItemData(@Nonnull ItemMeta meta) {
public @Nonnull Optional<String> 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<String> data1 = getItemData(meta1);
// Check if the first data is present
if (data1.isPresent()) {
// Only retrieve the second data where necessary.
Optional<String> 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;
}
}
}

View File

@ -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<String> possibleSfItemId = Slimefun.getItemDataService().getItemData(possibleSfItemMeta);
Optional<String> 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) {