From 969feb4a1c9d17d39aa29c610f828ca78ce088c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luiz=20Kr=C3=BCger?= <46863600+itisluiz@users.noreply.github.com> Date: Sat, 16 Jul 2022 17:30:20 -0300 Subject: [PATCH] ItemGroup info on getItemDetail (#1127) --- .../peripheral/generic/data/ItemData.java | 26 +++++++++++++++++++ .../generic/methods/InventoryMethods.java | 9 ++++++- .../resources/META-INF/accesstransformer.cfg | 4 +++ 3 files changed, 38 insertions(+), 1 deletion(-) 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 d39deec4a..21b936a14 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,6 +10,7 @@ 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.ItemGroup; import net.minecraft.item.ItemStack; import net.minecraft.nbt.CompoundNBT; import net.minecraft.nbt.INBT; @@ -67,6 +68,7 @@ public class ItemData } data.put( "tags", DataHelpers.getTags( stack.getItem().getTags() ) ); + data.put( "itemGroups", getItemGroups( stack ) ); CompoundNBT tag = stack.getTag(); if( tag != null && tag.contains( "display", Constants.NBT.TAG_COMPOUND ) ) @@ -116,6 +118,30 @@ public class ItemData } } + /** + * Retrieve all item groups an item stack pertains to. + * + * @param stack Stack to analyse + * @return A filled list that contains pairs of item group IDs and their display names. + */ + @Nonnull + private static List> getItemGroups( @Nonnull ItemStack stack ) + { + List> groups = new ArrayList<>( 1 ); + + for( ItemGroup group : stack.getItem().getCreativeTabs() ) + { + if( group == null ) continue; + + Map groupData = new HashMap<>( 2 ); + groupData.put( "id", group.langId ); + groupData.put( "displayName", group.displayName.getString() ); + groups.add( groupData ); + } + + return groups; + } + /** * Retrieve all visible enchantments from given stack. Try to follow all tooltip rules : order and visibility. * diff --git a/src/main/java/dan200/computercraft/shared/peripheral/generic/methods/InventoryMethods.java b/src/main/java/dan200/computercraft/shared/peripheral/generic/methods/InventoryMethods.java index 87f4c3952..f755d0420 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/generic/methods/InventoryMethods.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/generic/methods/InventoryMethods.java @@ -107,7 +107,9 @@ public class InventoryMethods implements GenericPeripheral * * The returned information contains the same information as each item in * {@link #list}, as well as additional details like the display name - * (`displayName`) and item durability (`damage`, `maxDamage`, `durability`). + * (`displayName`), item groups (`itemGroups`), which are the creative tabs + * an item will appear under, and item and item durability (`damage`, + * `maxDamage`, `durability`). * * Some items include more information (such as enchantments) - it is * recommended to print it out using @{textutils.serialize} or in the Lua @@ -127,6 +129,11 @@ public class InventoryMethods implements GenericPeripheral * * print(("%s (%s)"):format(item.displayName, item.name)) * print(("Count: %d/%d"):format(item.count, item.maxCount)) + * + * for _, group in pairs(item.itemGroups) do + * print(("Group: %s"):format(group.displayName)) + * end + * * if item.damage then * print(("Damage: %d/%d"):format(item.damage, item.maxDamage)) * end diff --git a/src/main/resources/META-INF/accesstransformer.cfg b/src/main/resources/META-INF/accesstransformer.cfg index 2b28a1390..55bf2c721 100644 --- a/src/main/resources/META-INF/accesstransformer.cfg +++ b/src/main/resources/META-INF/accesstransformer.cfg @@ -11,3 +11,7 @@ public net.minecraft.client.Minecraft field_71462_r # currentScreen # SpeakerInstance/SpeakerManager public net.minecraft.client.audio.SoundSource func_216421_a(I)V # pumpBuffers public net.minecraft.client.audio.SoundEngine field_217940_j # executor + +# ItemData +public net.minecraft.item.ItemGroup field_78034_o # langId +public net.minecraft.item.ItemGroup field_242391_q # displayName