Is there a way to take data of a Item?

Hello is there a way to take the data of an item/block? For example I need to save in a config file a block of sand, it can be red or normal but from getType.getName I only get sand. Same thing for wool or slab for example. Is there a Way?

Im assuming you mean api 7 (as that sort of data is in the named id in 1.13+)

From what i can remember, the best way is to save the BlockState directly using the TypeTokens class

https://jd.spongepowered.org/spongeapi/7.4.0/org/spongepowered/api/util/TypeTokens.html#BLOCK_TOKEN

Can you give me an example, please?

Its been a while. Here is the example from the docs modified to suit this

BlockState state = node.getValue(TypeTokens.BLOCK_TOKEN);

node.set(TypeTokens.BLOCK_TOKEN, state);

In typetokens I don’t find BLOCK_STATE_VALUE

Sorry i wasnt expecting you to do a copy and paste job. I dont like spoonfeeding people.

The javadocs i sent over in my first comment is the value you want

Thank you but maybe i’m not understanding good. I need to this: get the itemstack in the hand of a player and if it has a data like the sand or the stone save also the other Data. How can I do that?

I dont understand what you mean? Mind rephrasing?

From my understanding your wanting to save and load a itemstack. To which you can take the same approach however with the Item stack snapshot from the TypeTokens and save/load a snapshot of the itemstack. From there you can change that to a live item stack

So something like


Itemstack stack;
ItemStackSnapshot snapshot = stack.createSnapshot()
node.set(TypeTokens.ITEM_STACK_SNAPSHOT, snapshot);
ItemStackSnapshot snapshot = node.get(TypeTokens.ITEM_STACK_SNAPSHOT);
ItemStack stack = snapshot.createStack();

Also if you havent read it, here are the docs for api 7

https://docs.spongepowered.org/7.4.0/en-GB/plugin/configuration/index.html

For example I need to do the same thing I can do on Bukkit. On bukkit I can create an Itemstack with new ItemStack() and one of arguments is (byte) data. I want to to the same on sponge. And what is node you wrote in the code?

Item numeric ids were deprecated in minecraft prior to 1.8 (probably even more) so sponge has never officially supported them and never will.

There are ways to get those values, but i wouldn’t recommend it. When it comes to people changing config values, its easier to changing a white wool block to black wool by finding the wool type and changing the value to black then looking up online what the old 1.12.2 id code is for black wool and setting it