Skip to content

Use an expiring cache for MagicItems#getMagicItemDataFromItemStack #1036

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

Merged
merged 1 commit into from
Jun 21, 2025
Merged
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
4 changes: 4 additions & 0 deletions NOTICE.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,7 @@ The project includes software developed by third parties. For their full license
- packetevents:
- Repository: `retrooper/packetevents`
- License: **General Public License v3.0** (See `3rd_party_licenses/LICENSE-GPLv3`)

- Caffeine:
- Repository: `ben-manes/caffeine`
- License: **Apache-2.0 License** (See `3rd_party_licenses/LICENSE-Apache_v2`)
2 changes: 2 additions & 0 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ plugins {

dependencies {
shadow(group: "org.apache.commons", name: "commons-math4-core", version: "4.0-beta1")
shadow(group: "com.github.ben-manes.caffeine", name: "caffeine", version: "3.2.1")
shadow(group: "com.github.Chronoken", name: "EffectLib", version: "753da6c")
shadow(group: "co.aikar", name: "acf-paper", version: "0.5.1-SNAPSHOT")
shadow(group: "org.bstats", name: "bstats-bukkit", version: "3.0.2")
Expand Down Expand Up @@ -48,6 +49,7 @@ jar {
shadowJar {
configurations = [project.configurations.shadow]

relocate("com.github.benmanes.caffeine", "com.nisovin.magicspells.shaded.com.github.benmanes.caffeine")
relocate("org.apache.commons.math4", "com.nisovin.magicspells.shaded.org.apache.commons.math4")
relocate("de.slikey.effectlib", "com.nisovin.magicspells.shaded.effectlib")
relocate("co.aikar.commands", "com.nisovin.magicspells.shaded.acf")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.nisovin.magicspells.util.magicitems;

import java.util.*;
import java.time.Duration;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;

import net.kyori.adventure.text.Component;

Expand All @@ -25,7 +29,11 @@
public class MagicItems {

private static final Map<String, MagicItem> magicItems = new HashMap<>();
private static final Map<ItemStack, MagicItemData> itemStackCache = new HashMap<>();

private static final Cache<ItemStack, MagicItemData> itemStackCache = Caffeine.newBuilder()
.expireAfterAccess(Duration.ofMinutes(5))
.maximumSize(10000)
.build();

public static Map<String, MagicItem> getMagicItems() {
return magicItems;
Expand Down Expand Up @@ -58,37 +66,36 @@ public static MagicItemData getMagicItemDataByInternalName(String internalName)
return magicItems.get(internalName).getMagicItemData();
}

public static MagicItemData getMagicItemDataFromItemStack(ItemStack itemStack) {
if (itemStack == null) return null;
public static MagicItemData getMagicItemDataFromItemStack(ItemStack item) {
if (item == null) return null;

MagicItemData data = itemStackCache.getIfPresent(item);
if (data != null) return data;

MagicItemData cached = itemStackCache.get(itemStack);
// We can do this because itemStackCache doesn't have any null values
if (cached != null) return cached;
data = getMagicItemDataFromItemStackInternal(item);
itemStackCache.put(item.clone(), data);

return data;
}

private static MagicItemData getMagicItemDataFromItemStackInternal(ItemStack item) {
MagicItemData data = new MagicItemData();

// type
data.setAttribute(TYPE, itemStack.getType());

if (itemStack.getType().isAir()) {
itemStackCache.put(itemStack, data);
return data;
}
data.setAttribute(TYPE, item.getType());
if (item.getType().isAir()) return data;

// amount
data.setAttribute(AMOUNT, itemStack.getAmount());
data.setAttribute(AMOUNT, item.getAmount());

ItemMeta meta = itemStack.getItemMeta();
if (meta == null) {
itemStackCache.put(itemStack, data);
return data;
}
ItemMeta meta = item.getItemMeta();
if (meta == null) return data;

// name
NameHandler.processMagicItemData(meta, data);

// durability
if (itemStack.getType().getMaxDurability() > 0) DurabilityHandler.processMagicItemData(meta, data);
if (item.getType().getMaxDurability() > 0) DurabilityHandler.processMagicItemData(meta, data);

// repairCost
RepairableHandler.processMagicItemData(meta, data);
Expand Down Expand Up @@ -149,9 +156,8 @@ public static MagicItemData getMagicItemDataFromItemStack(ItemStack itemStack) {
BannerHandler.processMagicItemData(meta, data);

// block data
BlockDataHandler.processMagicItemData(meta, data, itemStack.getType());
BlockDataHandler.processMagicItemData(meta, data, item.getType());

itemStackCache.put(itemStack, data);
return data;
}

Expand Down