From fc4bc9aa0583de8f1cbaf2c6b7eaeb0566c90ef8 Mon Sep 17 00:00:00 2001 From: ToadDev <748280+Toad-Dev@users.noreply.github.com> Date: Thu, 10 Jun 2021 20:11:38 -0700 Subject: [PATCH] Fix #57 --- .../peripheral/generic/data/ItemData.java | 39 ++++++++++++++----- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/src/main/java/dan200/computercraft/shared/peripheral/generic/data/ItemData.java b/src/main/java/dan200/computercraft/shared/peripheral/generic/data/ItemData.java index bf002abac..f96f36219 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/generic/data/ItemData.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/generic/data/ItemData.java @@ -10,19 +10,19 @@ import dan200.computercraft.shared.util.NBTUtil; import net.minecraft.enchantment.Enchantment; import net.minecraft.enchantment.EnchantmentHelper; import net.minecraft.item.EnchantedBookItem; +import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.ListTag; +import net.minecraft.nbt.Tag; import net.minecraft.tag.ServerTagManagerHolder; -import net.minecraft.tag.Tag; +import net.minecraft.tag.TagGroup; import net.minecraft.text.Text; import javax.annotation.Nonnull; import javax.annotation.Nullable; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; +import java.util.stream.Collectors; /** * Data providers for items. @@ -66,7 +66,30 @@ public class ItemData if( stack.isDamaged() ) { - data.put( "durability", 1.0 - (stack.getDamage() / stack.getMaxDamage()) ); + data.put( "durability", (double) stack.getDamage() / stack.getMaxDamage() ); + } + + // requireNonNull is safe because we got the Identifiers out of the TagGroup to start with. Would be nicer + // to stream the tags directly but TagGroup isn't a collection :( + TagGroup itemTags = ServerTagManagerHolder.getTagManager().getItems(); + data.put( "tags", DataHelpers.getTags( itemTags.getTagIds().stream() + .filter( id -> Objects.requireNonNull( itemTags.getTag( id ) ).contains( stack.getItem() ) ) + .collect( Collectors.toList() ) + ) ); // chaos x2 + + CompoundTag tag = stack.getTag(); + if( tag != null && tag.contains( "display", NBTUtil.TAG_COMPOUND ) ) + { + CompoundTag displayTag = tag.getCompound( "display" ); + if( displayTag.contains( "Lore", NBTUtil.TAG_LIST ) ) + { + ListTag loreTag = displayTag.getList( "Lore", NBTUtil.TAG_STRING ); + data.put( "lore", loreTag.stream() + .map( ItemData::parseTextComponent ) + .filter( Objects::nonNull ) + .map( Text::getString ) + .collect( Collectors.toList() ) ); + } } /* @@ -74,7 +97,6 @@ public class ItemData * @see https://minecraft.gamepedia.com/Tutorials/Command_NBT_tags * @see ItemStack#getTooltip */ - CompoundTag tag = stack.getTag(); int hideFlags = tag != null ? tag.getInt( "HideFlags" ) : 0; List> enchants = getAllEnchants( stack, hideFlags ); @@ -85,11 +107,10 @@ public class ItemData data.put( "unbreakable", true ); } - data.put( "tags", DataHelpers.getTags( ServerTagManagerHolder.getTagManager().getItems().getTagsFor( stack.getItem() ) ) ); // chaos - return data; } + @Nullable private static Text parseTextComponent( @Nonnull Tag x ) {