InvHandler
Link to invhandler
Importing the class
Link to importing-the-class
It might be required for you to import the package if you encounter any issues (like casting an Array), so better be safe than sorry and add the import at the very top of the file.
ZenScript Copyimport mods.initialinventory.InvHandler;
Static Methods
Link to static-methods
Name: addRespawnItem
Adds an item to the player's inventory when they respawn after dying.
The slotIndex is where in the inventory the item will go.
By default, the slotIndex will be -1
, which means that it will try and fill an existing slot with the item, but if the player doesn't have the item, or if there is an overflow of items (if the player has 63 Diamonds and then receives 4 diamonds),
then the item is put into the first empty slot in the player's inventory, or spawned in the world around the player.
For example, if slot 0 was empty, and slot 1 had a Diamond, and the player then received a Diamond with an index of -1
, it will go into slot 1, filling the already existing stack, however, if slot 1 already had 64 diamonds, then it will go into slot 0.
The onGiven function is used to alter an item before it is given to a player, it can be used to add a level of randomization to the given items, you can change any aspect of the item (like count or Data) and you will have access to a random source through the world you can get from the player.
ZenScript Copy// InvHandler.addRespawnItem(item as IItemStack, slotIndex as int, onGiven as BiFunction<IItemStack,Player,IItemStack>)
InvHandler.addRespawnItem(<item:minecraft:diamond_sword>, 2, (stack as IItemStack, player as Player) as IItemStack => {
return stack.withDisplayName(new crafttweaker.api.text.TranslatableComponent("%s's sword", [player.name]));
});
Parameter | Type | Description | Optional | Default Value |
---|---|---|---|---|
Parameter item | Type IItemStack | Description The item to give to the player on initial join | Optional false | Default Value |
Parameter slotIndex | Type int | Description The slot to put the item into, or -1 | Optional true | Default Value -1 |
Parameter onGiven | Type BiFunction<IItemStack,Player,IItemStack> | Description The function to modify the item before it is given to the player. | Optional true | Default Value (stack as crafttweaker.api.item.IItemStack, player as crafttweaker.api.entity.type.player.Player) as crafttweaker.api.item.IItemStack => stack |
Name: addStartingItem
Adds an item to the player's inventory when they join a world.
The key is used as a flag that tracks if the player has previously received the item. This flag is used to prevent players from receiving duplicate items by joining the game multiple times.
Multiple items may share the same key however it is recommended to give each item a unique key or to group keys based on the pack version.
Following these recommendations will ensure that existing players from a previous version of the pack will still receive new items when updating the pack.
For example if version 1.0.0 of your pack gives the player a stick and an apple you could give both items the key "1.0.0".
Then if you later add a new item like a stone pickaxe in version 1.0.1 of your pack you would give it the key "1.0.1".
This will ensure that players who have already played 1.0.0 will receive the stone pickaxe when updating to 1.0.8.
The slotIndex is where in the inventory the item will go.
By default, the slotIndex will be -1
, which means that it will try and fill an existing slot with the item, but if the player doesn't have the item, or if there is an overflow of items (if the player has 63 Diamonds and then receives 4 diamonds),
then the item is put into the first empty slot in the player's inventory, or spawned in the world around the player.
For example, if slot 0 was empty, and slot 1 had a Diamond, and the player then received a Diamond with an index of -1
, it will go into slot 1, filling the already existing stack, however, if slot 1 already had 64 diamonds, then it will go into slot 0.
The onGiven function is used to alter an item before it is given to a player, it can be used to add a level of randomization to the given items, you can change any aspect of the item (like count or Data) and you will have access to a random source through the world you can get from the player.
ZenScript Copy// InvHandler.addStartingItem(key as string, item as IItemStack, slotIndex as int, onGiven as BiFunction<IItemStack,Player,IItemStack>)
InvHandler.addStartingItem("one", <item:minecraft:diamond_sword>, 2, (stack as crafttweaker.api.item.IItemStack, player as crafttweaker.api.entity.type.player.Player) as crafttweaker.api.item.IItemStack => {
return stack * (player.level.random.nextInt(6) + 1);
});
Parameter | Type | Description | Optional | Default Value |
---|---|---|---|---|
Parameter key | Type string | Description The key used to determine if the player has already received this item. | Optional false | Default Value |
Parameter item | Type IItemStack | Description The item to give to the player on initial join | Optional false | Default Value |
Parameter slotIndex | Type int | Description The slot to put the item into, or -1 | Optional true | Default Value -1 |
Parameter onGiven | Type BiFunction<IItemStack,Player,IItemStack> | Description The function to modify the item before it is given to the player. | Optional true | Default Value (stack as crafttweaker.api.item.IItemStack, player as crafttweaker.api.entity.type.player.Player) as crafttweaker.api.item.IItemStack => stack |