Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix errors in SlimefunUtils #4214

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
import org.bukkit.persistence.PersistentDataType;

import io.github.bakedlibs.dough.common.ChatColors;
import io.github.bakedlibs.dough.data.persistent.PersistentDataAPI;
import io.github.thebusybiscuit.slimefun4.api.SlimefunAddon;
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 io.github.thebusybiscuit.slimefun4.core.attributes.DistinctiveItem;
import io.github.thebusybiscuit.slimefun4.core.handlers.ItemUseHandler;
import io.github.thebusybiscuit.slimefun4.core.services.sounds.SoundEffect;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
Expand All @@ -38,7 +40,7 @@
*
* @see StormStaff
*/
public abstract class LimitedUseItem extends SimpleSlimefunItem<ItemUseHandler> {
public abstract class LimitedUseItem extends SimpleSlimefunItem<ItemUseHandler> implements DistinctiveItem {

private final NamespacedKey defaultUsageKey;
private int maxUseCount = -1;
Expand Down Expand Up @@ -66,7 +68,7 @@ public final int getMaxUseCount() {
*
* @param count
* The maximum number of times this item can be used.
*
*
* @return The {@link LimitedUseItem} for chaining of setters
*/
public final @Nonnull LimitedUseItem setMaxUseCount(int count) {
Expand Down Expand Up @@ -149,4 +151,15 @@ private void updateItemLore(ItemStack item, ItemMeta meta, int usesLeft) {
}
}

@Override
public boolean canStack(ItemMeta itemMetaOne, ItemMeta itemMetaTwo) {
if (!Slimefun.getItemDataService().getItemData(itemMetaOne).equals(Slimefun.getItemDataService().getItemData(itemMetaTwo))) {
return false;
}

NamespacedKey key = getStorageKey();
int usesLeft1 = PersistentDataAPI.getInt(itemMetaOne, key);
int usesLeft2 = PersistentDataAPI.getInt(itemMetaTwo, key);
return usesLeft1 == usesLeft2;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -388,29 +388,29 @@ public static boolean isItemSimilar(@Nullable ItemStack item, @Nullable ItemStac
* Slimefun items may be ItemStackWrapper's in the context of cargo
* so let's try to do an ID comparison before meta comparison
*/
Debug.log(TestCase.CARGO_INPUT_TESTING, " sfitem is ItemStackWrapper - possible SF Item: {}", sfitem);
Debug.log(TestCase.CARGO_INPUT_TESTING, " sfitem is ItemStackWrapper - possible SF Item: {}", item);

ItemMeta possibleSfItemMeta = sfitem.getItemMeta();
String id = Slimefun.getItemDataService().getItemData(itemMeta).orElse(null);
String possibleItemId = Slimefun.getItemDataService().getItemData(possibleSfItemMeta).orElse(null);
ItemMeta sfItemMeta = sfitem.getItemMeta();
String possibleItemId = Slimefun.getItemDataService().getItemData(itemMeta).orElse(null);
String sfItemId = Slimefun.getItemDataService()
.getItemData(sfItemMeta).get();
// Prioritize SlimefunItem id comparison over ItemMeta comparison
if (id != null && id.equals(possibleItemId)) {
Debug.log(TestCase.CARGO_INPUT_TESTING, " Item IDs matched!");
if (possibleItemId != null && possibleItemId.equals(sfItemId)) {
Debug.log(TestCase.CARGO_INPUT_TESTING, " SlimefunItem IDs matched!");

/*
* PR #3417
*
* Some items can't rely on just IDs matching and will implement {@link DistinctiveItem}
* in which case we want to use the method provided to compare
*/
Optional<DistinctiveItem> optionalDistinctive = getDistinctiveItem(id);
Optional<DistinctiveItem> optionalDistinctive = getDistinctiveItem(possibleItemId);
if (optionalDistinctive.isPresent()) {
return optionalDistinctive.get().canStack(possibleSfItemMeta, itemMeta);
return optionalDistinctive.get().canStack(sfItemMeta, itemMeta);
}
return true;
} else {
Debug.log(TestCase.CARGO_INPUT_TESTING, " Item IDs don't match, checking meta {} == {} (lore: {})", itemMeta, possibleSfItemMeta, checkLore);
return equalsItemMeta(itemMeta, possibleSfItemMeta, checkLore);
return false;
}
} else if (sfitem.hasItemMeta()) {
ItemMeta sfItemMeta = sfitem.getItemMeta();
Expand Down
Loading