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 an item that gives the player an extra heart when used, we first need to create a custom item class that extends Item and override the onItemUseFinish method. Then, we can increase the player's maximum health using the AttributeModifier. */ public class HeartItem extends Item { public HeartItem(Properties properties) { super(properties); } @Override public ItemStack onItemUseFinish(ItemStack stack, World world, LivingEntity entityLiving) { if (entityLiving instanceof PlayerEntity) { PlayerEntity player = (PlayerEntity) entityLiving; /* Increase the player's maximum health by 2 (1 heart) */ ModifiableAttributeInstance maxHealth = player.getAttribute(Attributes.MAX_HEALTH); maxHealth.addTransientModifier(new AttributeModifier(UUID.randomUUID(), "extra_heart", 2, AttributeModifier.Operation.ADDITION)); /* Heal the player for the amount of the extra heart */ player.heal(2); /* Decrease the item stack size by 1 */ stack.shrink(1); } return stack; } } /* Now, register the Heart item in your ItemInit class. */ public static final RegistryObject<Item> HEART_ITEM = ITEMS.register("heart", () -> new HeartItem(new Item.Properties().tab(ItemGroup.TAB_MISC)));