Try   HackMD

Minecraft Fabric Modding - 物品專欄

Item Bar

在自訂物品 class 中 override 三個函數:

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

@Override public void appendTooltip(ItemStack stack, @Nullable World world, List<Text> tooltip, TooltipContext context) {}

tooltip 就是我們要拿來操作的東西。加入程式碼:

tooltip.add(new LiteralText("Test Tooltip").formatted(Formatting.GREEN));

若有很多行,可重複 tooltip.add()

Text

常用的 Text 類別有 LiteralText 以及 TranslatableText,此二皆繼承 BaseText

  • BaseText implement MutableText extends Text

用法:

new LiteralText("Hello World!");

可以加入函數 formatted() 將文字新增色彩與樣式:

new LiteralText("Red and Bold").formatted(Formatting.RED, Formatting.BOLD);

若想在一串字內有不同樣式或者不同種類的 Text,可用函數 append()

new LiteralText("Apple: ").append( new TranslatableText("item.minecraft.apple").formatted(Formatting.YELLOW));

NBT

NBT (二進位命名標籤,Named Binary Tags),是 Minecraft 中用來存儲資料的一種資料格式,格式介紹可參考 Minecraft Wiki

Item v.s. ItemStack

Item 是那一個物品,所有同樣的 Item 都長得一樣;
但 ItemStack 是指那一坨東西,他可以擁有自己的 NBT、等等性質。

只有 Item Stack 有 NBT!
只有 Item Stack 有 NBT!
只有 Item Stack 有 NBT!

get NBT

ItemStack stack; // stack 通常是函數的參數 stack.getOrCreateNbt().getInt("key");

如果你是用 Intellij 的話,打完 stack 之後他可能會跳出提示 getNbt()getNbt() 有一個不好的地方就是他無法確定你這個物品是否有 NBT,如果沒有就會跳錯,因此我們會習慣用 getOrCreateNbt(),就算沒有 NBT,他也會自動幫我們新增一個。

put NBT

ItemStack stack; // stack 通常是函數的參數 stack.getOrCreateNbt().putInt("key", 100);

同樣的,我們使用 getOrCreateNbt() 來放入數值。

Commonly used functions

useOnBlock

public ActionResult useOnBlock(ItemUsageContext context) { return ActionResult.PASS; }

ActionResult

  • PASS 允許物品在副手

useOnEntity

public ActionResult useOnEntity(ItemStack stack, PlayerEntity user, LivingEntity entity, Hand hand) { return ActionResult.PASS; }

use

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

public boolean onStackClicked(ItemStack stack, Slot slot, ClickType clickType, PlayerEntity player) {}

postHit & postMine

public boolean postHit(ItemStack stack, LivingEntity target, LivingEntity attacker) {} public boolean postMine(ItemStack stack, World world, BlockState state, BlockPos pos, LivingEntity miner) {}

finishUsing

public ItemStack finishUsing(ItemStack stack, World world, LivingEntity user) {}

onCraft

public void onCraft(ItemStack stack, World world, PlayerEntity player) {}

getEnchantbility

public int getEnchantability() {}

物品能付到好的附魔的程度

Wood Stone Iron Diamond Gold
15 5 14 10 22

canRepair

public boolean canRepair(ItemStack stack, ItemStack ingredient) {}

tags: Minecraft Fabric Modding