Blockstate in an itemstack

Started by ImEvolutiONx on

Topic category: Help with Minecraft modding (Java Edition)

Joined Dec 2024
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Blockstate in an itemstack

Hello,

I've been trying to mod things that were -apparently- not accessible with MCreator. However, I absolutely cannot use java lang so I sticked with MCreator but using custom code snippets. I'm currently adding genetics to crops that allow different effects when eaten. To allow the drop of those crops to keep the "genetic tags" when harvested, I used the function copy_state of the loottable i used. Therefore, I stored everything in block_states. Itemstacks can have block_states, stored in their DataComponents but except for "custom_data", these are not accessible with MCreator (or I didn't find a way). Still, I managed to build custom code snippets to access it (based on itemstack.get(DataComponents.BLOCK_STATE). Everything works fine and I can loop from itemstack to growing crop while keeping the blockstates. However, when giving myself from creative tab the item or the crop, their block_states doesn't exist. This causes crashes as when the player tries to plant the crop, there's nothing to get() on this itemstack.

I want to implement a default data components to my items but I don't know how to do it. I found this : https://docs.neoforged.net/docs/items/datacomponents/#adding-default-data-components-to-items so I guess that's possible ! The problem is that I don't know where to put that code (just in a 'custom element' in mcreator ?) and how to change it to my items/components/key_values because I'm not sure how to read it.

 

If anyone can help me at least to understand this and where to put it in mcreator to execute it that would be great !

Thanks a lot : )

 

 

PS : 

Mod name --> ecoevocraft

Item name --> ginger_rhizome

blockstate 1 --> nutritional_value ; default_value --> "very_low"   (I'm planning to set this to an integer and not an enum)

blockstate 2 --> regen ; default_value --> 0

Joined Dec 2024
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Quick update :  I tried…
Sun, 12/29/2024 - 18:25

Quick update : 

I tried implementing the code

// For some DeferredRegister.Items REGISTRAR
public static final Item COMPONENT_EXAMPLE = REGISTRAR.register("component",
    // register is used over other overloads as the DataComponentType has not been registered yet
    registryName -> new Item(
        new Item.Properties()
        .setId(ResourceKey.create(Registries.ITEM, registryName))
        .component(BASIC_EXAMPLE.get(), new ExampleRecord(24, true))
    )
);

Inside the .java of the item by changing some of the things that seemed weird to me (and did not resulted into a crash) but the only thing I couldn't figure out is what they meant by REGISTRAR (I tried reading the Registries tab but didn't make it work inside the .java)

Joined Dec 2024
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Resolved :    I added this…
Mon, 12/30/2024 - 17:05

Resolved : 

 

I added this simple line into the [youritemid]Item.java (accessible either from your workspace or mcreator --> the considered item --> right click --> open mod element in code editor --> [youritemid]Item.java

 

public class GingerRhizomeItem extends Item {
public GingerRhizomeItem() {
super(new Item.Properties()
.stacksTo(64)
.rarity(Rarity.COMMON)
.component(BLOCK_STATE, new BlockItemStateProperties(Map.of(k1, v1, k2, v2))));
}

Now whenever I give myself from commands or from creative inventory, the item has an additional component (which is BLOCK_STATE defined by a Map.of() which seems like the format of a matrix ; my matrix here was pair key_values --> "nutritional_value", "0", "regen", "0" --> this sets those 2 values to 0 by default, allowing overwritting)

 

/!\ when checking the data of an item stored in inventory in-game while debugging (/data get entity Dev) the item shows up but not its "block_state" within its data_components. They are -apparently- still stored as I could retrieve them and print them in the console.