NBTItem problem
Kyumar opened this issue ยท 33 comments
Server version: 1.12.2
api version: 2.8.0
I am applying a nbt to an item using NBTItem when the item is dropped, everything works correctly, dropping an item with another nbt (applied by another plugin) dropping the item you lose everything, name, lore etc, ideas?
Check that you are updating the item in the inventory. NBTItem is a copy of the item in the inventory. Also print out the data before and after dropping. Noteworthy that the nbtapi has no event hooks, so basically has to be another plugin screwing up.
when player drop item, execute that
public void apply(ItemStack itemStack, int id){
NBTItem nbtItem = new NBTItem(itemStack);
nbtItem.setInteger("dropID", id);
nbtItem.applyNBT(itemStack);
}
when i pickup it nothing changes, when i test it in another server, if i drop an item with an nbt, all data disappears
Thats not really the intended way to do that^^. Modify the data in the NBTItem and then event.setItemStack(nbtItem.getItem()
. Also note the javadoc https://github.com/tr7zw/Item-NBT-API/blob/master/item-nbt-api/src/main/java/de/tr7zw/changeme/nbtapi/NBTItem.java#L64 .
No.
public void apply(ItemStack itemStack, int id){
NBTItem nbtItem = new NBTItem(itemStack, true);
nbtItem.setInteger("dropID", id);
}
So, if i have to add an nbt to an item who might have custom nbt should i use mergeCustomNBT# ?
You best case don't use any of these methods. Either use .getItem() to get back the modified item, or add true
to the constructor of NBTItem to enable direct apply mode.
public ItemStack apply(ItemStack itemStack, int id){
NBTItem nbtItem = new NBTItem(itemStack, true);
nbtItem.setInteger("dropID", id);
nbtItem.mergeCustomNBT(itemStack);
return nbtItem.getItem();
}
something like that?
So what could the solution be? At the drop I generate an id and insert everything in the db, to take into account that that particular id is associated with that drop, how do I do it?
https://www.toptal.com/developers/hastebin/onaramudux.java that's the apply# code
https://www.toptal.com/developers/hastebin/ebaqulipol.csharp that's the drop event code where is fired apply#
Do you perhaps mixup the items? Also don't mix changing nbt and itemmeta at the same time.
Ehhhh that code shouldn't work at all? You are trying to update the event after it happened. Also, you could just store all of the data you are putting into the database onto the item^^
Yes, but your event handler already technically shouldn't work and can have really bad side effects, because you are using an (async) task there.
I need to async only the insert query in the database, everything else doing it syn should work as then?
No what I'm saying is that your logic doesn't work. Yes you need to do the SQL stuff async, but that also means you can not use that during events like this. Your logic of "when something drops, note that in the database and then write it to the item" will just not work. You have a few options: ditch the database in general and just write that data to the item/item entity, generate random ids/keep track of the database ids and set it on the item/item entity before you are writing it to the database.
Hey sorry, even doing so continues to remove the other nbt tags and put only dropID
I mean what are you even trying to achieve? Because at pickup you have the reverse issue of your SQL query will happen in the future after the person/hopper/entity has picked up the item.
Again, why do it this way then xD On drop write to the entity!(not the item stack) who dropped it and when. And at pickup get that data + who picked it up and when and write it async to the database.
Ah wait you are in 1.12, then you need to write it to the item instead of the entity.
i need a database beacause i have to get all drops of a certain hour or of certain players, all works, the only problem was this nbt thing
The issue is that your code breaks the spigot API logic. Please add a few
System.out.println(new NBTItem(event.getItemStack));
to a) before you do anything b) your async logic c) after updating the item.