MinecraftScripterAI
Discord ↗
Link Copied to Clipboard
×
"Make an item with the name Heart and as soon as you use it you get an extra heart"
/* To create a custom item called Heart that gives the player an extra heart when used, you'll need to create a new class that extends Item and override the onItemUse method. */ public class HeartItem extends Item { public HeartItem(Properties properties) { super(properties); } @Override public ActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) { ItemStack itemStack = player.getItemInHand(hand); if (!world.isClientSide) { // Increase the player's max health by 2 (1 heart) player.getAttribute(Attributes.MAX_HEALTH).addPermanentModifier(new AttributeModifier(UUID.randomUUID(), "extra_heart", 2, AttributeModifier.Operation.ADDITION)); // Consume the Heart item if (!player.abilities.instabuild) { itemStack.shrink(1); } } return ActionResult.success(itemStack); } } /* To register the Heart item, add the following code to your item registration class: */ public static final RegistryObject<Item> HEART = ITEMS.register("heart", () -> new HeartItem(new Item.Properties().tab(ItemGroup.TAB_MISC)));