NBT comparing, breaks on reload

Discussion in 'Plugin Development' started by PhantomUnicorns, Jul 17, 2017.

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

    PhantomUnicorns

    I'm trying to test if a item matches another item, pretty simple. The items have a display name and NBT. Using ItemStack#isSimilar(ItemStack) it's pretty easy. But once I add NBT, it works except when I reload my server. Then it doesn't work, and only works for the newest version of the item (I give the item onEnable to all players, I set the NBT the same exact way each time (I don't change the code at all)). When I don't add NBT, it works. Anyone know why/how to fix? If so, I would like a solution that takes into account NBT values.

    I'll be able to respond in about 20-40minutes, sorry for the delay!
     
  2. Offline

    Zombie_Striker

    @PhantomUnicorns
    Are you sure the NBT tags are carrying over across reloads? It sounds as though one of the itemstack's you're comparing is missing that NBT tag.
     
  3. Offline

    PhantomUnicorns

    I'm sure, because I'm setting the generic.armor for the slot mainhand, and it still works (increasing my armor). But it doesn't register as the same item. It's weird because #isSimilar doesn't even look at NBT data
     
    Last edited: Jul 17, 2017
  4. Offline

    AlvinB

    @PhantomUnicorns
    Actually, the implementation of isSimilar in CraftItemStack looks at NBT tags. From CraftItemStack#isSimilar():
    Code:java
    1. this.handle.getTag().equals(that.handle.getTag())
    This would strongly suggest that the tags aren't saving properly. Can we see your code?

    When I use this code, everything works flawlessly, I see that the NBT tags are there after the restart, AND they pass isSimilar():
    Code:java
    1. @Override
    2. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    3. if (!(sender instanceof Player)) {
    4. return true;
    5. }
    6. if (args.length == 1) {
    7. Player player = ((Player) sender);
    8. switch (args[0]) {
    9. case "additem": {
    10. ItemStack itemStack = new ItemStack(Material.BLAZE_ROD);
    11. NBTUtil.NBTTagCompound compound = new NBTUtil.NBTTagCompound();
    12. compound.setString("testString", "abc...åäö");
    13. itemStack = NBTUtil.setItemNBT(itemStack, compound);
    14. player.getInventory().addItem(itemStack);
    15. break;
    16. }
    17. case "compare": {
    18. ItemStack itemStack = player.getInventory().getItemInMainHand();
    19. ItemStack itemStack2 = player.getInventory().getItemInOffHand();
    20. sender.sendMessage(String.valueOf(itemStack.isSimilar(itemStack2)));
    21. break;
    22. }
    23. case "printNBT": {
    24. ItemStack itemStack = player.getInventory().getItemInMainHand();
    25. ItemStack itemStack2 = player.getInventory().getItemInOffHand();
    26. sender.sendMessage("Main hand: " + NBTUtil.getItemNBT(itemStack));
    27. sender.sendMessage("Off hand: " + NBTUtil.getItemNBT(itemStack2));
    28. break;
    29. }
    30. }
    31. }
    32. return true;
    33. }
     
  5. Offline

    PhantomUnicorns

    EDIT: I see why it's going bad :p, when I set the generic.armor to the slot mainhand, I have to supply a random uuid least and most. This uuid is a new random each time, but is requried to set it. I'm trying to think of a fix of it now, if anyone has a fix then my ears are open. Just to clear up what I said:
    Code:
    SETINT_METHOD.invoke(comp, "UUIDLeast", (int) uuid.getMostSignificantBits());
    SETINT_METHOD.invoke(comp, "UUIDMost", (int) uuid.getMostSignificantBits());
    
    That's some reflection code that I use in it, it's doing:
    Code:
    comp.setInt("UUIDLeast", (int) uuid.getMostSignificantBits());
    comp.setInt("UUIDMost", (int) uuid.getMostSignificantBits());
    
    UUID being:
    Code:
    UUID uuid = UUID.randomUUID();
    
    The different UUID's I found that it returns:
    • 3372873788163047964
    • 808927740789542364
    • It can return more, just showing why it's not returning true for isSimilar
    EDIT: can anyone tell me why UUIDLeast and UUIDMost even need to be set/set to? I get the other tags I'm setting, but I don't get that one (I went with the flow of the tutorials I found online).

    EDIT: I got it to work! But if anyone could explain UUIDLeast and UUIDMost it would be a lifesaver!

    EDIT: Ik now that UUIDLeast and UUIDMost matter because if items have the same of both, they don't work together (They don't stack effects!)
     
    Last edited: Jul 18, 2017
  6. Offline

    AlvinB

    @PhantomUnicorns
    Well, write your own isSimilar method where you check everything just like before but you don't compare the UUID bits?
     
  7. Offline

    PhantomUnicorns

    Yeah that's what I'm going to do once I have time :p
     
Thread Status:
Not open for further replies.

Share This Page