#Cannot compare PDC INTEGER DATA TYPE

1 messages · Page 1 of 1 (latest)

viral vapor
#
data.get(new NamespacedKey(GioxMC.getPlugin(), "magnet"), PersistentDataType.INTEGER)```
When being printed out to the console, return 0, but when you try to compare it with 0, doesn't work.

Any help on how to fix it ?
#
if(data.get(new NamespacedKey(GioxMC.getPlugin(), "magnet"), PersistentDataType.INTEGER) == 0) return;```
This should be able to compare the value that it return to 0, but it doesn't work.
Is there a specific way of comparing PDCs ?
potent haven
#

Show your code you use to set it

viral vapor
#
data.set(new NamespacedKey(GioxMC.getPlugin(), "magnet"), PersistentDataType.INTEGER, 0);```
blazing shuttle
#

is this going through yaml serialisation ?

viral vapor
#

No?

#

As far as I know.

blazing shuttle
#

I mean, if data.get(new NamespacedKey(GioxMC.getPlugin(), "magnet"), PersistentDataType.INTEGER) prints 0 to the console for you

#

then data.get(new NamespacedKey(GioxMC.getPlugin(), "magnet"), PersistentDataType.INTEGER) == 0 should be true

potent haven
#

Last I used PDC it worked fine on Integers

blazing shuttle
#

it should auto unbox here

potent haven
#

this is in my dataType Integer type = primitive.get(key("type"), PersistentDataType.INTEGER);

#

works fine

viral vapor
blazing shuttle
#

those all look fine. Are you sure that your if statement is failing here

#

e.g. print data.get(new NamespacedKey(GioxMC.getPlugin(), "magnet"), PersistentDataType.INTEGER) == 0 to your logger

viral vapor
#

Yeap

#
System.out.println(1);
        if(item.getItemMeta() == null) return;
        PersistentDataContainer data = item.getItemMeta().getPersistentDataContainer();
        System.out.println(2);
        if(data.get(new NamespacedKey(GioxMC.getPlugin(), "owner"), PersistentDataType.STRING) == null) return;
        if(data.get(new NamespacedKey(GioxMC.getPlugin(), "magnet"), PersistentDataType.INTEGER) == 0) return; // <(==
        System.out.println(3);```
potent haven
#

you sure its not exiting on owner?

viral vapor
#

Nope.

#

Let me try it again tho.

#

Again, the same thing.

blazing shuttle
#

Eh I mean, save the return val into a variable, print it and its type and then also it compared to 0 using the == operator

viral vapor
#

huuuuh ???

potent haven
#

You did get me to spot an error in my PDC DataType code though

viral vapor
#

I'm really wondering why this thing wont work.

potent haven
#

Really no clue, its workgin fine here

#

unless.

#

did you at some point write that key with a different data type?

viral vapor
#

Nope, it's INTEGER.

potent haven
#

it has always been INTEGER?

viral vapor
#

Correct.

#

Even setting the value of it, doesn't work.

potent haven
viral vapor
#

There is literally no error.

potent haven
#

its underlined, shows a warning?

viral vapor
#
@EventHandler
    void playerClickEvent(InventoryClickEvent e) {
        Inventory i = e.getClickedInventory();
        ItemStack itemClicked = e.getCurrentItem();
        InventoryView view = e.getView();

        if (i == e.getWhoClicked().getInventory()) return;
        if (itemClicked == null) return;
        if (view.getTitle().equalsIgnoreCase("Upgrade Menu")) {
            if (e.getSlot() == 20)
                upgradeMagnet((Player) e.getWhoClicked(), e.getWhoClicked().getInventory().getItemInMainHand());
            if (e.getSlot() == 30)
                upgradeBlessing((Player) e.getWhoClicked(), e.getWhoClicked().getInventory().getItemInMainHand());
        }
    }

    public void upgradeMagnet(Player p, ItemStack item) {
        if(item.getItemMeta() == null) return;
        PersistentDataContainer data = item.getItemMeta().getPersistentDataContainer();
        if (!data.has(new NamespacedKey(GioxMC.getPlugin(), "owner"), PersistentDataType.STRING)) return;
        if (!data.get(new NamespacedKey(GioxMC.getPlugin(), "owner"), PersistentDataType.STRING).equalsIgnoreCase(p.getUniqueId().toString())) {
            p.sendMessage(GioxMC.prefix + "This hoe can only be upgraded by it's owner.");
            return;
        }
        if (data.get(new NamespacedKey(GioxMC.getPlugin(), "magnet"), PersistentDataType.INTEGER) == 1) {
            p.sendMessage(GioxMC.prefix + "The charm that you're trying to update is already on it's max level.");
            return;
        } else {
            data.set(new NamespacedKey(GioxMC.getPlugin(), "magnet"), PersistentDataType.INTEGER, 1);
            p.sendMessage(GioxMC.prefix + "Magnet upgrated to level " + 1);
        }
    }```
#

This is the code.

viral vapor
tacit rampart
#

I think you forgot to set the ItemMeta back to the item

potent haven
#

that you did

#

also, I recommend you start using some has methods

#

you don;t have to get to check if it exists, there is a has method

#

same for item.hasItemMeta()

viral vapor
#

ohh okay

potent haven
#

let us know if putting teh meta back fixes this, so we can leave the thread

viral vapor
#

My server crashed, but I think it did.

#

The meta may've been the problem.

#

Coz I never actually updated it's meta.

#

Thinking that it's the Itemstack itself handling it.

potent haven
#

Sounds promising

#

this is also why has is a nice method. it literally tells you if it exists, any value

viral vapor
#

Yes it works.

#

The problem was the ItemMeta itself.

potent haven
#

nice

viral vapor
#

I forgot to set it, therefor the value was never updated.

potent haven
#

yep