From c00df8461f20d945ea75b41e2d32005574f44415 Mon Sep 17 00:00:00 2001 From: Aaron <51387595+AzureAaron@users.noreply.github.com> Date: Tue, 24 Jun 2025 01:25:33 -0400 Subject: Add support for NEU Repo item overlays (aka modern items) --- .../skyblock/itemlist/ItemRepository.java | 2 + .../skyblock/itemlist/StackOverlays.java | 60 ++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 src/main/java/de/hysky/skyblocker/skyblock/itemlist/StackOverlays.java (limited to 'src/main/java/de') diff --git a/src/main/java/de/hysky/skyblocker/skyblock/itemlist/ItemRepository.java b/src/main/java/de/hysky/skyblocker/skyblock/itemlist/ItemRepository.java index f771aa45..5572e030 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/itemlist/ItemRepository.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/itemlist/ItemRepository.java @@ -54,6 +54,8 @@ public class ItemRepository { private static void loadItem(NEUItem item) { try { ItemStack stack = ItemStackBuilder.fromNEUItem(item); + StackOverlays.applyOverlay(item, stack); + items.add(stack); itemsMap.put(item.getSkyblockItemId(), stack); } catch (Exception e) { diff --git a/src/main/java/de/hysky/skyblocker/skyblock/itemlist/StackOverlays.java b/src/main/java/de/hysky/skyblocker/skyblock/itemlist/StackOverlays.java new file mode 100644 index 00000000..04a67d1c --- /dev/null +++ b/src/main/java/de/hysky/skyblocker/skyblock/itemlist/StackOverlays.java @@ -0,0 +1,60 @@ +package de.hysky.skyblocker.skyblock.itemlist; + +import java.nio.file.Files; + +import org.slf4j.Logger; + +import com.mojang.logging.LogUtils; + +import de.hysky.skyblocker.utils.NEURepoManager; +import io.github.moulberry.repo.NEURepoFile; +import io.github.moulberry.repo.data.NEUItem; +import net.minecraft.component.ComponentChanges; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NbtOps; +import net.minecraft.nbt.StringNbtReader; + +/** + * Handles applying "overlays" to modern {@code ItemStack}s from the NEU Repository. Overlays are already in the modern components + * format according to the data version of the given directory which matches the vanilla data version number. This system allows the + * NEU Repository to provide support for modern items while maintaining backwards compatibility with 1.8 and older modern releases (say 1.21.1). + * + * Note that overlays do not contain all the original components, lore and custom name are notably left out for ease of maintenance. + */ +public class StackOverlays { + private static final Logger LOGGER = LogUtils.getLogger(); + /** Data Version for 1.21.5 */ + private static final int DATA_VERSION = 4325; + private static final String OVERLAY_DIRECTORY = "itemsOverlay/" + DATA_VERSION; + + /** + * Applies the necessary overlay for the {@code stack} if applicable. + */ + protected static void applyOverlay(NEUItem neuItem, ItemStack stack) { + try { + NEURepoFile file = NEURepoManager.NEU_REPO.file(OVERLAY_DIRECTORY + "/" + neuItem.getSkyblockItemId() + ".snbt"); + + //The returned file is null if it does not exist + if (file != null) { + //Read the overlay file and parse an ItemStack from it + String overlayData = Files.readString(file.getFsPath()); + ItemStack overlayStack = ItemStack.CODEC.parse(NbtOps.INSTANCE, StringNbtReader.readCompound(overlayData)) + .setPartial(ItemStack.EMPTY) + .resultOrPartial(error -> logParseError(neuItem, error)) + .get(); + + if (!overlayStack.isEmpty()) { + //Apply the component changes from the overlay stack + ComponentChanges changes = overlayStack.getComponentChanges(); + stack.applyChanges(changes); + } + } + } catch (Exception e) { + LOGGER.error("[Skyblocker Stack Overlays] Failed to apply stack overlay! Item: {}", neuItem.getSkyblockItemId(), e); + } + } + + private static void logParseError(NEUItem neuItem, String message) { + LOGGER.error("[Skyblocker Stack Overlays] Failed to parse item \"{}\". Error: {}", neuItem.getSkyblockItemId(), message); + } +} -- cgit