Want a better Minecraft server? Read about SpigotMC here!
Separate names with a comma.
Discussion in 'Spigot Plugin Development' started by Agamer64, Aug 1, 2020 at 12:02 AM.
How can I put an item into an npc's hand using only nms?
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.
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.
... 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.
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!
For correct answers look down below since @Blutkrone knows everything better.
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.
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:
CraftLivingEntity cle = (CraftLivingEntity) npc.getBukkitEntity();
ItemStack itemstack = new ItemStack(Material.DIAMOND_SWORD,1);
npc = (EntityPlayer) cle.getHandle();
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?
I don't know if you also want to use nms here but this is for spigot: AttribueInstance#setBaseValue(double value)
In your case AttribueInstance. setBaseValue(7.0D)
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.