Items changes

This commit is contained in:
Auxilor
2022-02-01 13:17:52 +00:00
parent 5302aa07a5
commit 9992820580
4 changed files with 127 additions and 40 deletions

View File

@@ -0,0 +1,102 @@
package com.willfp.eco.core.items;
import com.willfp.eco.core.fast.FastItemStack;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
/**
* An item and its {@link com.willfp.eco.core.fast.FastItemStack} hash.
*/
public final class HashedItem {
/**
* The item.
*/
private final ItemStack item;
/**
* The hash.
*/
private final int hash;
/**
* Create new hashed item.
*
* @param item The item.
* @param hash The hash.
*/
public HashedItem(@NotNull final ItemStack item,
final int hash) {
this.item = item;
this.hash = hash;
}
/**
* Get the item.
*
* @return The ItemStack.
*/
public ItemStack getItem() {
return this.item;
}
/**
* Get the hash.
*
* @return The hash.
*/
public int getHash() {
return this.hash;
}
/**
* Kotlin destructuring support.
*
* @return The ItemStack.
*/
public ItemStack component1() {
return this.item();
}
/**
* Kotlin destructuring support.
*
* @return The hash.
*/
public int component2() {
return this.hash();
}
@Override
public int hashCode() {
return this.hash;
}
@Override
public boolean equals(@Nullable final Object other) {
if (!(other instanceof HashedItem o)) {
return false;
}
return o.hash == this.hash;
}
/**
* Hashed item from an item.
*
* @param item The item.
* @return The hashed item.
*/
public static HashedItem of(@NotNull final ItemStack item) {
return new HashedItem(item, FastItemStack.wrap(item).hashCode());
}
/**
* Hashed item from a fast item stack.
*
* @param item The item.
* @return The hashed item.
*/
public static HashedItem of(@NotNull final FastItemStack item) {
return new HashedItem(item.unwrap(), item.hashCode());
}
}

View File

@@ -1,5 +1,8 @@
package com.willfp.eco.core.items;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.willfp.eco.core.fast.FastItemStack;
import com.willfp.eco.core.items.args.LookupArgParser;
import com.willfp.eco.core.items.provider.ItemProvider;
@@ -25,6 +28,7 @@ import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;
import java.util.stream.Collectors;
@@ -38,9 +42,27 @@ public final class Items {
private static final Map<NamespacedKey, TestableItem> REGISTRY = new ConcurrentHashMap<>();
/**
* Cached custom item lookups, using {@link FastItemStack#hashCode()}.
* Cached custom item lookups, using {@link HashedItem}.
*/
private static final Map<Integer, Optional<TestableItem>> CACHE = new ConcurrentHashMap<>();
private static final LoadingCache<HashedItem, Optional<TestableItem>> CACHE = CacheBuilder.newBuilder()
.expireAfterAccess(5, TimeUnit.MINUTES)
.build(
new CacheLoader<>() {
@Override
@NotNull
public Optional<TestableItem> load(@NotNull final HashedItem key) {
TestableItem match = null;
for (TestableItem item : REGISTRY.values()) {
if (item.matches(key.item())) {
match = item;
break;
}
}
return Optional.ofNullable(match);
}
}
);
/**
* All item providers.
@@ -297,26 +319,7 @@ public final class Items {
public static CustomItem getCustomItem(@NotNull final ItemStack itemStack) {
int hash = FastItemStack.wrap(itemStack).hashCode();
if (CACHE.containsKey(hash)) {
return CACHE.get(hash).map(Items::getOrWrap).orElse(null);
}
TestableItem match = null;
for (TestableItem item : REGISTRY.values()) {
if (item.matches(itemStack)) {
match = item;
break;
}
}
// Cache even if not matched; allows for marking hashes as definitely not custom.
CACHE.put(hash, Optional.ofNullable(match));
if (match == null) {
return null;
}
return getOrWrap(match);
return CACHE.getUnchecked(HashedItem.of(itemStack)).map(Items::getOrWrap).orElse(null);
}
/**
@@ -350,14 +353,6 @@ public final class Items {
}
}
/**
* Clear the lookup cache.
*/
@ApiStatus.Internal
public static void clearCache() {
CACHE.clear();
}
private Items() {
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
}