# Minecraft Fabric Modding - 物品專欄
## Item Bar
在自訂物品 class 中 override 三個函數:
```java=
public boolean isItemBarVisible(ItemStack stack) {
return true;
}
public int getItemBarStep(ItemStack stack) {
return Math.round((0.1) * 12);
}
public int getItemBarColor(ItemStack stack) {
return MathHelper.packRgb(R, G, B);
}
```
`isItemBarVisible` 是決定 ItemBar 的顯示與否
`getItemBarStep` 則是決定每次改變要改變多少的 pixel
`getItemBarColor` 則是 ItemBar 的顏色
※特別注意:
1. `getItemBarStep` 傳回整數型態,須以 round 做轉換
2. `packRgb` 中的 RGB 需填入 0~1 的浮點數 (float)
## Tooltip
先 Override Item 固有的函數 `appendTooltip`
```java=
@Override
public void appendTooltip(ItemStack stack, @Nullable World world, List<Text> tooltip, TooltipContext context) {}
```
tooltip 就是我們要拿來操作的東西。加入程式碼:
```java=
tooltip.add(new LiteralText("Test Tooltip").formatted(Formatting.GREEN));
```
若有很多行,可重複 `tooltip.add()`
### Text
常用的 `Text` 類別有 `LiteralText` 以及 `TranslatableText`,此二皆繼承 `BaseText`。
- `BaseText` implement `MutableText` extends `Text`
用法:
```java=
new LiteralText("Hello World!");
```
可以加入函數 `formatted()` 將文字新增色彩與樣式:
```java=
new LiteralText("Red and Bold").formatted(Formatting.RED, Formatting.BOLD);
```
若想在一串字內有不同樣式或者不同種類的 Text,可用函數 `append()`
```java=
new LiteralText("Apple: ").append(
new TranslatableText("item.minecraft.apple").formatted(Formatting.YELLOW));
```
## NBT
NBT (二進位命名標籤,Named Binary Tags),是 Minecraft 中用來存儲資料的一種資料格式,格式介紹可參考 [Minecraft Wiki](https://minecraft.fandom.com/zh/wiki/NBT%E6%A0%BC%E5%BC%8F?variant=zh-tw)
### Item v.s. ItemStack
Item 是那一個物品,所有同樣的 Item 都長得一樣;
但 ItemStack 是指那一坨東西,他可以擁有自己的 NBT、等等性質。
***只有 Item Stack 有 NBT!***
***只有 Item Stack 有 NBT!***
***只有 Item Stack 有 NBT!***
### get NBT
```java=
ItemStack stack; // stack 通常是函數的參數
stack.getOrCreateNbt().getInt("key");
```
如果你是用 Intellij 的話,打完 stack 之後他可能會跳出提示 `getNbt()`,`getNbt()` 有一個不好的地方就是他無法確定你這個物品是否有 NBT,如果沒有就會跳錯,因此我們會習慣用 `getOrCreateNbt()`,就算沒有 NBT,他也會自動幫我們新增一個。
### put NBT
```java=
ItemStack stack; // stack 通常是函數的參數
stack.getOrCreateNbt().putInt("key", 100);
```
同樣的,我們使用 `getOrCreateNbt()` 來放入數值。
## Commonly used functions
### useOnBlock
```java=
public ActionResult useOnBlock(ItemUsageContext context) {
return ActionResult.PASS;
}
```
#### ActionResult

- PASS 允許物品在副手
### useOnEntity
```java=
public ActionResult useOnEntity(ItemStack stack, PlayerEntity user, LivingEntity entity, Hand hand) {
return ActionResult.PASS;
}
```
### use
```java=
public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand hand) {}
```
| TypedActionResult\<T\> |
| ---------------------- |
| success(T data) |
| consume(T data) |
| pass(T data) |
| fail(T data) |
### onStackClicked
```java=
public boolean onStackClicked(ItemStack stack, Slot slot, ClickType clickType, PlayerEntity player) {}
```
### postHit & postMine
```java=
public boolean postHit(ItemStack stack, LivingEntity target, LivingEntity attacker) {}
public boolean postMine(ItemStack stack, World world, BlockState state, BlockPos pos, LivingEntity miner) {}
```
### finishUsing
```java=
public ItemStack finishUsing(ItemStack stack, World world, LivingEntity user) {}
```
### onCraft
```java=
public void onCraft(ItemStack stack, World world, PlayerEntity player) {}
```
### getEnchantbility
```java=
public int getEnchantability() {}
```
物品能付到好的附魔的程度
| Wood | Stone | Iron | Diamond | Gold |
| ---- | ----- | ---- | ------- | ---- |
| 15 | 5 | 14 | 10 | 22 |
### canRepair
```java=
public boolean canRepair(ItemStack stack, ItemStack ingredient) {}
```
---
###### tags: `Minecraft Fabric Modding`