diff options
| author | Aaron <51387595+AzureAaron@users.noreply.github.com> | 2024-08-18 16:37:20 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-18 22:37:20 +0200 |
| commit | 4a4165ab8b4ead2e6a47b51ba56331cadb656333 (patch) | |
| tree | 70e85ba83df8dbf7b1c1b0a02c5332c4d53d98cb /src/main/java | |
| parent | 31af537e8ea86c2208ca75f708d55845408c94be (diff) | |
| download | Skyblocker-4a4165ab8b4ead2e6a47b51ba56331cadb656333.tar.gz Skyblocker-4a4165ab8b4ead2e6a47b51ba56331cadb656333.tar.bz2 Skyblocker-4a4165ab8b4ead2e6a47b51ba56331cadb656333.zip | |
Proper Item DFU for PV (#913)
* Proper Item DFU for PV
This is the result of wayy too much functional programming with DFU (or
not enough!)
* Fix Enchantment Glint Bug + 1.0.0 of Legacy Item DFU
* Make position optional
:3
Diffstat (limited to 'src/main/java')
3 files changed, 96 insertions, 97 deletions
diff --git a/src/main/java/de/hysky/skyblocker/skyblock/profileviewer/inventory/itemLoaders/ItemLoader.java b/src/main/java/de/hysky/skyblocker/skyblock/profileviewer/inventory/itemLoaders/ItemLoader.java index cd1a8e23..fd4d3f4c 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/profileviewer/inventory/itemLoaders/ItemLoader.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/profileviewer/inventory/itemLoaders/ItemLoader.java @@ -5,31 +5,19 @@ import com.google.gson.JsonParser; import com.mojang.serialization.JsonOps; import de.hysky.skyblocker.skyblock.PetCache; -import de.hysky.skyblocker.skyblock.itemlist.ItemRepository; import de.hysky.skyblocker.skyblock.profileviewer.ProfileViewerScreen; import de.hysky.skyblocker.skyblock.profileviewer.inventory.Pet; import de.hysky.skyblocker.skyblock.tabhud.util.Ico; import de.hysky.skyblocker.utils.ItemUtils; -import de.hysky.skyblocker.utils.NEURepoManager; -import de.hysky.skyblocker.utils.TextTransformer; -import io.github.moulberry.repo.data.NEUItem; +import de.hysky.skyblocker.utils.datafixer.LegacyItemStackFixer; import net.minecraft.component.DataComponentTypes; -import net.minecraft.component.type.*; -import net.minecraft.datafixer.fix.ItemIdFix; -import net.minecraft.datafixer.fix.ItemInstanceTheFlatteningFix; import net.minecraft.item.ItemStack; import net.minecraft.nbt.*; -import net.minecraft.registry.Registries; import net.minecraft.text.Text; -import net.minecraft.util.Formatting; -import net.minecraft.util.Identifier; import net.minecraft.util.Util; import java.io.ByteArrayInputStream; import java.util.*; -import java.util.stream.Collectors; - -import static de.hysky.skyblocker.skyblock.itemlist.ItemRepository.getItemStack; public class ItemLoader { @@ -38,94 +26,37 @@ public class ItemLoader { List<ItemStack> itemList = new ArrayList<>(); for (int i = 0; i < containerContent.size(); i++) { - if (containerContent.getCompound(i).getInt("id") == 0) { + NbtCompound nbt = containerContent.getCompound(i); + if (nbt.getInt("id") == 0) { itemList.add(ItemStack.EMPTY); continue; } - NbtCompound nbttag = containerContent.getCompound(i).getCompound("tag"); - NbtCompound extraAttributes = nbttag.getCompound("ExtraAttributes"); - String internalName = extraAttributes.getString("id"); - if (internalName.equals("PET")) { - PetCache.PetInfo petInfo = PetCache.PetInfo.CODEC.parse(JsonOps.INSTANCE, JsonParser.parseString(extraAttributes.getString("petInfo"))).getOrThrow(); - Pet pet = new Pet(petInfo); - itemList.add(pet.getIcon()); - continue; - } + ItemStack stack = LegacyItemStackFixer.fixLegacyStack(nbt); - Identifier itemId = identifierFromOldId(containerContent.getCompound(i).getInt("id"), containerContent.getCompound(i).getInt("Damage")); + if (stack.isEmpty()) { + ItemStack fallback = Ico.BARRIER.copy(); - ItemStack stack; - if (itemId.toString().equals("minecraft:air")) { - ItemStack itemStack = getItemStack(internalName); - stack = itemStack != null ? itemStack.copy() : ItemStack.EMPTY; - } else { - stack = new ItemStack(Registries.ITEM.get(itemId)); - } + fallback.set(DataComponentTypes.CUSTOM_NAME, Text.literal("Error: " + nbt.getCompound("tag").getCompound("ExtraAttributes").getString("id"))); + itemList.add(fallback); - if (stack.isEmpty() || stack.getItem().equals(Ico.BARRIER.getItem())) { - // Last ditch effort to find item in NEU REPO - Map<String, NEUItem> items = NEURepoManager.NEU_REPO.getItems().getItems(); - stack = items.values().stream() - .filter(j -> Formatting.strip(j.getSkyblockItemId()).equals(Formatting.strip(internalName).replace(":", "-"))) - .findFirst() - .map(NEUItem::getSkyblockItemId) - .map(ItemRepository::getItemStack) - .map(ItemStack::copy) - .orElse(Ico.BARRIER.copy()); - - - if (stack.getName().getString().contains("barrier")) { - stack.set(DataComponentTypes.CUSTOM_NAME, Text.literal("Err: " + internalName)); - itemList.add(stack); - continue; - } + continue; } - // Custom Data - NbtCompound customData = new NbtCompound(); + String itemId = stack.getSkyblockId(); + NbtCompound customData = ItemUtils.getCustomData(stack); - // Add Skyblock Item Id - customData.put(ItemUtils.ID, NbtString.of(internalName)); - - - // Item Name - stack.set(DataComponentTypes.CUSTOM_NAME, TextTransformer.fromLegacy(nbttag.getCompound("display").getString("Name"))); - - // Lore - NbtList loreList = nbttag.getCompound("display").getList("Lore", 8); - stack.set(DataComponentTypes.LORE, new LoreComponent(loreList.stream() - .map(NbtElement::asString) - .map(TextTransformer::fromLegacy) - .collect(Collectors.toList()))); - - // add skull texture - NbtList texture = nbttag.getCompound("SkullOwner").getCompound("Properties").getList("textures", 10); - if (!texture.isEmpty()) { - stack.set(DataComponentTypes.PROFILE, new ProfileComponent(Optional.of(internalName), Optional.of(UUID.fromString(nbttag.getCompound("SkullOwner").get("Id").asString())), ItemUtils.propertyMapWithTexture(texture.getCompound(0).getString("Value")))); - } - - // Colour - if (nbttag.getCompound("display").contains("color")) { - int color = nbttag.getCompound("display").getInt("color"); - stack.set(DataComponentTypes.DYED_COLOR, new DyedColorComponent(color, false)); - } - - // add enchantment glint - if (nbttag.getKeys().contains("ench")) { - stack.set(DataComponentTypes.ENCHANTMENT_GLINT_OVERRIDE, true); + if (itemId.equals("PET")) { + PetCache.PetInfo petInfo = PetCache.PetInfo.CODEC.parse(JsonOps.INSTANCE, JsonParser.parseString(customData.getString("petInfo"))).getOrThrow(); + Pet pet = new Pet(petInfo); + itemList.add(pet.getIcon()); + continue; } - // Hide weapon damage and other useless info - stack.set(DataComponentTypes.ATTRIBUTE_MODIFIERS, new AttributeModifiersComponent(List.of(), false)); - - // Set Count - stack.setCount(containerContent.getCompound(i).getInt("Count")); - // Attach an override for Aaron's Mod so that these ItemStacks will work with the mod's features even when not in Skyblock - extraAttributes.put("aaron-mod", Util.make(new NbtCompound(), comp -> comp.putBoolean("alwaysDisplaySkyblockInfo", true))); - - stack.set(DataComponentTypes.CUSTOM_DATA, NbtComponent.of(extraAttributes)); + if (stack.contains(DataComponentTypes.CUSTOM_DATA)) { + customData.put("aaron-mod", Util.make(new NbtCompound(), comp -> comp.putBoolean("alwaysDisplaySkyblockInfo", true))); + } itemList.add(stack); } @@ -133,14 +64,6 @@ public class ItemLoader { return itemList; } - private static Identifier identifierFromOldId(int id, int damage) { - try { - return damage != 0 ? Identifier.of(ItemInstanceTheFlatteningFix.getItem(ItemIdFix.fromId(id), damage)) : Identifier.of(ItemIdFix.fromId(id)); - } catch (Exception e) { - return Identifier.of("air"); - } - } - private static NbtList decompress(JsonObject data) { try { return NbtIo.readCompressed(new ByteArrayInputStream(Base64.getDecoder().decode(data.get("data").getAsString())), NbtSizeTracker.ofUnlimitedBytes()).getList("i", NbtElement.COMPOUND_TYPE); diff --git a/src/main/java/de/hysky/skyblocker/skyblock/special/DyeSpecialEffects.java b/src/main/java/de/hysky/skyblocker/skyblock/special/DyeSpecialEffects.java index abaac713..6fd8b568 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/special/DyeSpecialEffects.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/special/DyeSpecialEffects.java @@ -22,7 +22,7 @@ public class DyeSpecialEffects { private static final Logger LOGGER = LogUtils.getLogger(); private static final MinecraftClient CLIENT = MinecraftClient.getInstance(); @VisibleForTesting - protected static final Pattern DROP_PATTERN = Pattern.compile("WOW! (?:\\[[A-Z+]+\\] )?(?<player>[A-Za-z0-9_]+) found (?<dye>[A-Za-z ]+ Dye) #[\\d,]+!"); + protected static final Pattern DROP_PATTERN = Pattern.compile("WOW! (?:\\[[A-Z+]+\\] )?(?<player>[A-Za-z0-9_]+) found (?<dye>[A-Za-z ]+ Dye)(?: #[\\d,]+)?!"); static void init() { ClientReceiveMessageEvents.GAME.register(DyeSpecialEffects::displayDyeDropEffect); diff --git a/src/main/java/de/hysky/skyblocker/utils/datafixer/LegacyItemStackFixer.java b/src/main/java/de/hysky/skyblocker/utils/datafixer/LegacyItemStackFixer.java new file mode 100644 index 00000000..8a5c0430 --- /dev/null +++ b/src/main/java/de/hysky/skyblocker/utils/datafixer/LegacyItemStackFixer.java @@ -0,0 +1,76 @@ +package de.hysky.skyblocker.utils.datafixer; + +import static net.azureaaron.legacyitemdfu.LegacyItemStackFixer.getFixer; + +import java.util.List; + +import org.slf4j.Logger; + +import static net.azureaaron.legacyitemdfu.LegacyItemStackFixer.FIRST_VERSION; +import static net.azureaaron.legacyitemdfu.LegacyItemStackFixer.LATEST_VERSION; + +import com.mojang.logging.LogUtils; +import com.mojang.serialization.Dynamic; + +import de.hysky.skyblocker.utils.TextTransformer; +import net.azureaaron.legacyitemdfu.TypeReferences; +import net.minecraft.component.DataComponentTypes; +import net.minecraft.component.type.AttributeModifiersComponent; +import net.minecraft.component.type.ItemEnchantmentsComponent; +import net.minecraft.component.type.LoreComponent; +import net.minecraft.component.type.NbtComponent; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NbtCompound; +import net.minecraft.nbt.NbtElement; +import net.minecraft.nbt.NbtOps; +import net.minecraft.registry.RegistryOps; +import net.minecraft.text.Text; + +public class LegacyItemStackFixer { + private static final Logger LOGGER = LogUtils.getLogger(); + + //Static import things to avoid class name conflicts + @SuppressWarnings("deprecation") + public static ItemStack fixLegacyStack(NbtCompound nbt) { + RegistryOps<NbtElement> ops = ItemStackComponentizationFixer.getRegistryLookup().getOps(NbtOps.INSTANCE); + Dynamic<NbtElement> fixed = getFixer().update(TypeReferences.LEGACY_ITEM_STACK, new Dynamic<>(ops, nbt), FIRST_VERSION, LATEST_VERSION); + ItemStack stack = ItemStack.CODEC.parse(fixed) + .setPartial(ItemStack.EMPTY) + .resultOrPartial(LegacyItemStackFixer::log) + .get(); + + //Don't continue fixing up if it failed + if (stack.isEmpty()) return stack; + + if (stack.contains(DataComponentTypes.CUSTOM_NAME)) { + stack.set(DataComponentTypes.CUSTOM_NAME, TextTransformer.fromLegacy(stack.get(DataComponentTypes.CUSTOM_NAME).getString())); + } + + if (stack.contains(DataComponentTypes.LORE)) { + List<Text> fixedLore = stack.get(DataComponentTypes.LORE).lines().stream() + .map(Text::getString) + .map(TextTransformer::fromLegacy) + .map(Text.class::cast) + .toList(); + + stack.set(DataComponentTypes.LORE, new LoreComponent(fixedLore)); + } + + //Remap Custom Data + if (stack.contains(DataComponentTypes.CUSTOM_DATA)) { + stack.set(DataComponentTypes.CUSTOM_DATA, NbtComponent.of(stack.get(DataComponentTypes.CUSTOM_DATA).getNbt().getCompound("ExtraAttributes"))); + } + + //Hide Vanilla Attributes + stack.set(DataComponentTypes.ATTRIBUTE_MODIFIERS, AttributeModifiersComponent.DEFAULT.withShowInTooltip(false)); + + //Hide Vanilla Enchantments + stack.set(DataComponentTypes.ENCHANTMENTS, stack.getOrDefault(DataComponentTypes.ENCHANTMENTS, ItemEnchantmentsComponent.DEFAULT).withShowInTooltip(false)); + + return stack; + } + + private static void log(String error) { + LOGGER.error("[Skyblocker Legacy Item Fixer] Failed to fix up item! Error: {}", error); + } +} |
