ItemStack#getType() returns ItemBlock{Name=null}


#1

Ok, so whenever I want to check itemstack’s ItemType, I get a weird response…

AFAIK it should be “ItemStacks.LAPIS_BLOCK” or something similar…

Why doesn’t it do that?

for(int i = 0; i < inventory.capacity(); i++) {
    Optional<ItemStack> is = inventory.query(QueryOperationTypes.INVENTORY_PROPERTY.of(SlotIndex.of(i))).peek();
    if(is.isPresent()){
        str.append(is.get().getType());
    }
}

#2

Nevermind… Figured it out… Apparently, you have to do
ItemStack#getType()#getId()
to get the same ID minecraft uses…

100% unnecessary, but fine.


#3

I wouldn’t say unnessery. With the idea of overriding toString() it imply that your able to get all the data related to that BlockType as thats what the toString was essentially designed for. Using getId not only allows you to have an interface that allows Sponge to group together all ID based systems in minecraft (BlockType, ItemType, StoneType, etc) and 100% guarantee there is consisteny between them all, but also as a developer I personally tend to ignore getString as most developers dont override it, so having the id in its own function makes it clear to me and many other developers that that function gets the id of that BlockType.


#4

Now that you explain it like that, I understand… It’s just that if I use the method “getType()”, I’d like to get ItemType back, and not something else.


#5

As you mentioned in another thread. You came from Bukkit. Here with Sponge you will find things to give you more options.

If it were like what you want. Aka the following code

String id = stack.getType();

Then how would I get the item types name? The only logical thing I can think of creating another function to get the display name, but then you have 2 or more functions refering to a single type that isnt really directly related to the object. Thats why these are grouped together in there own object that can be retreived from the original object. Yes its slightly more work but it makes everything a lot cleaner to write and believe me, it doesn’t take long to get used to, so much so that when I write a bukkit plugin now, all i see are things that need to be changed or even added.


#6

Once again, fair point… It’s just that it’s different… We’ll all learn with time :stuck_out_tongue:


#7

When you use getType, you do get ItemType. Your error is expecting that toString returns anything useful. Never assume, for any object, that toString returns anything useful.