Create a item with a custom name

Hi there !

I’m trying to find a way to create an item (a nether star) with a custom name.

I’m new in Sponge plugin creation so it’s maybe a silly question.

Thanks for future anwsers.

Nanocryk

Right now, Inventory hasn’t been implemented. As soon as it it, I believe you will be able to do what you want.

Ok thanks for the quick anwser :slight_smile:

You don’t need it implemented in order to develop a plugin using it.

ItemStack specialItem = game.getRegistry()
                       .getItemBuilder()
                       .itemType(ItemTypes.NETHER_STAR)
                       .build();
DisplayNameData nameData = specialItem.getOrCreate(DisplayNameData.class).get();
specialItem.offer(nameData.setDisplayName(Texts.of("Custom Name")));

Yeah but how are you going to give it to a player?

Using the Inventory API

Here’s one way (there’s other ways too).

player.getInventory().query(Hotbar.class).first().set(specialItem);

I thought InventoryAPI wasn’t implemented, or do I just not know something? :blush:

It’s not, but it’s completely possible to develop a plugin using it. You just can’t test it yet.

1 Like

Alright, thanks simon.

There is a difference between not implemented and non-existent, the API does have things that have not been completely added to the implementation. This may limit testing but developers can still prototype code.

1 Like

Yeah, I know, but it was put into a context that made it seem like it was implemented.

How to set display name in last API ?

The method setDisplayName(Text.Literal) is undefined for the type DisplayNameData

You get the Value<Text> for the manipulator, then set the contained Text
Something like:

holder.getData(DisplayNameData.class).get().displayName().set(Texts.of("Something"));

Alternatively, you can set the Value directly

holder.set(Keys.DISPLAY_NAME, Texts.of("Something"));

Note that I have not tested this and it may not compile, but I hope you get the idea.

For future DataAPI 2.0 related questions, please use the following thread:

Thanks you. Works.