1.16.1 NMS Item in npc hand

Discussion in 'Spigot Plugin Development' started by Agamer64, Aug 1, 2020 at 12:02 AM.

  1. How can I put an item into an npc's hand using only nms?
    • Agree Agree x 1
  2. That is packets. not nms lol. If you are running IDEA, adding a dependcy for the nms version would allow you to track down where the spigot api method leads simply by chasing upwards on the methods CTRL-ALT-B presents me with whatever implements the things.
  3. net.minecraft.server.v_1_16_R1.PacketPlayOutEntityEquipment
    Packets are part of nms. It's completely legal to use packets if someone says NMS

    You should also not tell him any Hot keys because the settings of the guys that will write (or reading this in the future) here has almost not the same settings as you. Instead of that you should tell him the Execute action, that happens, if you press this. Because those who looking here for help. Are not the best using hotkeys. And maybe sth like that could crash their code.

    If he won't use packets: Take a look into the EntityArmorStand class. You should find a method there, that will be look like setting an item to the armorstand.
    #4 Player_Schark, Aug 1, 2020 at 12:46 PM
    Last edited: Aug 1, 2020 at 12:51 PM
    • Optimistic Optimistic x 1
  4. ... This isn't even an argument, calling packets a part of nms. Of course they are a part of nms, but you use packets to fake something to the client not actually "do" something. Not even worth arguing over, suggesting packets when someone asks for a nms solution is just plainly the wrong answer.

    And crashing their code is ... Alright, I really don't have anything to tell you here. Anyone who has actively modified the keymappings is either a completely lost case or already is familiar enough with the IDE to customize it for their needs and subsequently knowledgable enough to search for what the actual hotkey happens to be.

    Don't try to nitpick to defend your wrong answer.
  5. EDIT: i am immature for calling someone out on rude behavior so this is wrong
    First of all quit being rude. I am ashamed to the fact we live in the same country!
    Then if you cant stop being disrespectful, at least add ANY VALUE to your claims! All you did is throw empty arguments at @Player_Schark with no proof / evidence what so ever!
    And lastly your post could be marked offtopic, since you didn't even try to answer the question this thread is about!

    Done with the rant!

    EDIT: this is also wrong since packets are parts of NMS but apparently not NMS!
    Now to the actual problem. As @Player_Schark already wrote using the PacketPlayOutEntityEquipment packet is perfectly fine. It achieves what you want to achieve, which is guessing from your initial question "to display a different item for different players in some entities hand"

    Only the player the packet is sent to will see the defined item:
    Here a quick example using v1_16_R1

    EDIT: apparently i am "spoonfeeding" so no simple example!
    Code (Java):

    For correct answers look down below since @Blutkrone knows everything better.
    #6 zM4xi, Aug 1, 2020 at 1:55 PM
    Last edited: Aug 1, 2020 at 2:40 PM
    • Funny Funny x 1
    • Optimistic Optimistic x 1
  6. Where did you even pull "to display a different item for different players in some entities hand" from, why are you guys suggesting packets when OP asked for the nms method to change the item in the hand of an NPC?! Packets do NOT change the item an entity is holding? And thank you very much for spoonfeeding people with code, great choice to teach people by presenting them with code that can just be copy-pasted.

    The CraftLivingEntity (Any LivingEntity can be cast into it), can use the CraftLivingEntity#getEquipment method to fetch a CraftEntityEquipment instance. You can follow after the implementation of it to find that they call CraftEntityEquipment#setEquipment which is proxy call fetching the handle and calling the setSlot method on it.

    The correct answer for this thread is the EntityLiving#setSlot(EnumItemSlot slot, ItemStack item) method. The original implementation also has a call with CraftItemStack#asNMSCopy which may be necessary to be used aswell.

    And don't pull the card of being rude, I am perfectly civil here. You were the one randomly jumping into the thread and insulting random people who actually gave the proper answer, and proceed to spread additional misinformation. When you work with packets you would require to maintain the updates manually upon entity loading, newer and less experienced people may end up confused why the item they added to the entity isn't found on the server.

    As for your claims of me being disrespectful, offtopic and ashamed of living in the same country. Very mature of you.
    #7 Blutkrone, Aug 1, 2020 at 2:24 PM
    Last edited: Aug 1, 2020 at 2:35 PM
    • Useful Useful x 1
  7. I completely understand how to do it visually, but I do want to make the npc variable actually contain a diamond sword as what it is holding. The reason being is that I want to get the generic.attack_damage of the EntityPlayer. I went through most of the methods for entityplayer and none of them actually "saved" the item it was holding when I used system.out.print(EntityPlayer) Though hopefully the methods you sent me will work. Thank you all for your help!

    EDIT: I was able to successfully do this by converting from entityplayer to CraftLivingEntity then back to entityplayer! Originally, when I did entityplayer.getItemInMainHand().setItem(item) and many similar methods, it would never properly save. When I used this code:

    Code (Text):
            //Sets equipment
            CraftLivingEntity cle = (CraftLivingEntity) npc.getBukkitEntity();
            ItemStack itemstack = new ItemStack(Material.DIAMOND_SWORD,1);
            npc = (EntityPlayer) cle.getHandle();
    It worked!

    EDIT #2: When I get the generic.attack_damage value it returns 1.0 when it should be returning 7.0, does anyone know how I would fix this?
    #8 Agamer64, Aug 2, 2020 at 2:39 AM
    Last edited: Aug 2, 2020 at 3:17 AM
  8. Well I am unsure if the item that the npc will have, so that is why I want to get their attack damage value. I think it would be best if I just stored the attack damage values of each item in the game that does damage.
    • Like Like x 1