Access a vanilla minecraft custom item tag value

Discussion in 'Plugin Development' started by Liontack, Sep 24, 2020.

Thread Status:
Not open for further replies.
  1. Offline

    Liontack

    Hi, I am Liontack and I started developing my first bukkit plugin 2 weeks ago. I am new here. I figured out everything I needed until now. Now I need some help from more expierenced bukkit plugin developers.

    I created a villager in minecraft with the following command:
    /summon villager ~ ~ ~ {Invulnerable:1, PersistenceRequired:1, Inventory:[{id:"compass", Count:1, tag:{customTagName:"stringTagValue"}}]}

    What I want to achieve is to get the value "stringTagValue" in a java String variable in my plugin.

    What I could find to be the solution, possibly, is:
    String javaString = villager.getInventory().getItem(0).getItemMeta().getPersistentDataContainer().getOrDefault(NamespacedKey.minecraft("customTagName"), PersistentDataType.STRING, null);
    Which gives me the null value, because the PersistentDataContainer has no keys.

    I found multiple other questions while searching for my answer and I frequently encountered:
    CraftItemStack.asNMSCopy(item)
    But I couldn't really try this out. I have no CraftItemStack class and I have no clue in which library I can find this.

    I am working with new versions: Minecraft 1.16.2 with the newest bukkit API from two-ish weeks ago. I believe this is just a random build, not a major version.

    I quite like that the value stringTagValue is a NBT value stored in the world and I can change it while in creative mode. (I can change the value while the server is running.) I only need to read this value, I don't need to change it.

    Any thoughts or solutions are welcome! May it be to get this way working or an alternative way to achieve the same. If I missed some crucial information to answer, please ask for it! Thanks for reading my problem!
     
  2. Offline

    gochi9

    Since you said tha you do not have the CraftItemStack class than i imagine that you are using CraftBukkit. You should use spigot,it's both better performance wise and has a better API.
    After you change the server jar and the compile jar for the plugin you can create an NMS ItemStack and get the tag value.

    EDIT:
    Example

    Code:
            ItemStack item = villager.getInventory().getItem(0);
            net.minecraft.server.v1_16_R2.ItemStack nmsItemStack = CraftItemStack.asNMSCopy(item);
            NBTTagCompound itemCompound = (nmsItemStack.hasTag()) ? nmsItemStack.getTag() : new NBTTagCompound();
            if(!itemCompound.hasKey("stringTagValue")) return;
            String value = itemCompound.getString("stringTagValue");
    
    Also here is a more detailed tutorial: link
     
    Last edited: Sep 27, 2020
    Liontack likes this.
  3. Offline

    Liontack

    Thanks for your response gochi9!

    I have looked into CraftBukkit vs spigot now and I get really confused.
    I made my Paper plugin from IntelliJ via the IntelliJ plugin "Minecraft Development".
    This gives me a default setup to work in and the following dependency in the pom.xml:
    Code:
    <dependencies>
        <dependency>
            <groupId>com.destroystokyo.paper</groupId>
            <artifactId>paper-api</artifactId>
            <version>1.16.3-R0.1-SNAPSHOT</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    
    This gives access to packages org.bukkit and org.spigotmc. I thought that Paper was a fork of Spigot and Spigot a fork of Bukkit. And thus I would have access to all of spigot and bukkit. I thought my problem was with something in the bukkit sources.

    I've also found that the package of CraftItemStack should be org.bukkit.craftbukkit.inventory. And I will need the class net.minecraft.server.ItemStack too. Neither of these packages are accesible in my project setup.

    I will now look into other ways to setup my development environment. Do you, or anyone, know if I can run a spigot plugin in a papermc server? Or maybe a link to either setup a proper development environment for a spigot or paper plugin. Either way I think I better ask for support in this area on the spigot or paper forums.
     
    Last edited: Sep 25, 2020
  4. Offline

    gochi9

    @Liontack yes paper is better than spigot and you can access the bukit and spigot api from it.You can run a spigot plugin on a paper server but you cannot run a paper plugin on a spigot server. Also i think you are using the wrong jar to compile or something like that because you should be able to access the net.minecraft.server class from paper
     
    Liontack likes this.
Thread Status:
Not open for further replies.

Share This Page