zh_cn:tutorial:datagen_loot
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| zh_cn:tutorial:datagen_loot [2023/02/17 14:58] – created hanatomizu | zh_cn:tutorial:datagen_loot [2025/04/02 01:15] (current) – [方块战利品表] solidblock | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ======战利品表生成====== | + | ====== 战利品表生成 ====== |
| - | 在阅读本教程之前,请确认你已经阅读 [[zh_cn: | + | 在本教程,你将学习如何使用数据生成器为方块和其他内容生成战利品表。使用数据生成,就不再需要为每个方块写 JSON。在阅读本教程之前,请确认你已经阅读 [[datagen_setup|开始了解数据生成]],并且已经有一个实现了 '' |
| 首先,创建一个类(或者多个,你需要一个给方块、箱子和实体)来继承(extends) '' | 首先,创建一个类(或者多个,你需要一个给方块、箱子和实体)来继承(extends) '' | ||
| - | ====初始化==== | + | ===== 方块战利品表 |
| + | 要为方块战利品表添加数据生成,只需要创建一个继承了 '' | ||
| - | 要开始使用方块战利品掉落,要先创建一个战利品表生成器 | + | <code java TutorialBlockLootTableProvider.java> |
| + | public class TutorialBlockLootTableProvider extends FabricBlockLootTableProvider { | ||
| + | protected TutorialBlockLootTableProvider(FabricDataOutput dataOutput, CompletableFuture< | ||
| + | super(dataOutput, | ||
| + | } | ||
| - | <code java> | + | |
| - | private static class MyBlockLootTables extends SimpleFabricLootTableProvider { | + | public void generate() { |
| - | public MyBlockLootTables(FabricDataGenerator dataGenerator) { | + | // ... |
| - | | + | } |
| - | } | + | |
| - | + | ||
| - | | + | |
| - | public void accept(BiConsumer< | + | |
| - | // ... | + | |
| - | } | + | |
| } | } | ||
| + | </ | ||
| - | // ... | + | 然后,将数据生成添加到入口点中(这里我们用的例子是 '' |
| + | <code java ExampleModDataGenerator.java> | ||
| + | public class ExampleModDataGenerator implements DataGeneratorEntrypoint { | ||
| + | @Override | ||
| + | public void onInitializeDataGenerator(FabricDataGenerator generator) { | ||
| + | FabricDataGenerator.Pack pack = generator.createPack(); | ||
| - | @Override | + | pack.addProvider(TutorialBlockLootTableProvider::new); |
| - | public void onInitializeDataGenerator(FabricDataGenerator fabricDataGenerator) { | + | } |
| - | fabricDataGenerator.addProvider(MyBlockLootTables::new); | + | |
| } | } | ||
| </ | </ | ||
| - | 让我们为战利品表创建一个简单的矿石方块和它的掉落物。添加到你的方块初始化中,或者本实例中的 | + | > 如果使用的是 1.20 之前的版本,请将 '' |
| - | <code java> | + | 在此前的[[blocks|方块]]教程中,我们创建了一个// |
| - | public | + | < |
| - | + | | |
| - | public static final Item TEST_ITEM = Registry.register(Registry.ITEM, | + | |
| - | // 请不要在乎没有这个方块的细节 😅 | + | |
| + | } | ||
| </ | </ | ||
| - | ====添加方块战利品掉落==== | + | 这是方块最简单的战利品表。被挖掘时,掉落一个。在带有损耗的爆炸中被炸掉时(例如苦力怕、下界床),会被完成破坏而不掉落。 |
| + | 也可以弄得复杂一点点,比如(这里的代码仅用作举例,请不要为一个方块多次添加战利品表): | ||
| <code java> | <code java> | ||
| - | private static class MyBlockLootTables extends SimpleFabricLootTableProvider { | + | // 什么也不掉落 |
| - | | + | |
| - | super(dataGenerator, LootContextTypes.BLOCK); | + | |
| - | | + | // 掉落泥土方块 |
| - | + | addDrop(TutorialBlocks.EXAMPLE_BLOCK, Blocks.DIRT); | |
| - | | + | |
| - | | + | |
| - | // The BlockLootTableGenerator class contains a behemoth of utility methods. Just take some time and go through the methods available to override. | + | |
| - | | + | |
| - | } | + | // 只有在使用带有精准采集的工具时掉落自身,否则掉落泥土方块 |
| - | } | + | addDropWithSilkTouch(TutorialBlocks.EXAMPLE_BLOCK, |
| + | |||
| + | // 只有在使用剪刀或带有精准采集的工具时掉落自身,否则不掉落 | ||
| + | addDrop(TutorialBlocks.EXAMPLE_BLOCK, dropsWithSilkTouchOrShears(TutorialBlocks.EXAMPLE_BLOCK)); | ||
| + | |||
| + | // 掉落五个方块 | ||
| + | addDrop(TutorialBlocks.EXAMPLE_BLOCK, | ||
| + | | ||
| </ | </ | ||
| - | 好啦,我们现在成功地添加了一个方块,然后让我们创建一个箱子战利品掉落。 | + | 也可以为我们此前介绍的[[crops|自定义作物]]生成战利品表: |
| - | + | ||
| - | ====添加箱子战利品掉落==== | + | |
| - | + | ||
| - | 在开始的时候,我们需要一个标识。这个标识将会指向装有你的箱子掉落物的容器的 json 文件。 | + | |
| <code java> | <code java> | ||
| - | // 在 Tutorial 类中 | + | addDrop(TutorialBlocks.CUSTOM_CROP, cropDrops(TutorialBlocks.CUSTOM_CROP, |
| - | public static final Identifier TEST_CHEST = new Identifier(" | + | |
| </ | </ | ||
| - | 让我们一起创建一个箱子战利品表,然后像这样注册它: | + | ===== 简单战利品表 |
| - | <code java> | + | 要添加一个简单的战利品表,我们只需要继承 '' |
| + | <code java TutorialChestLootTableProvider.java> | ||
| + | public class TutorialChestLootTableProvider extends SimpleFabricLootTableProvider { | ||
| + | public TutorialChestLootTableProvider(FabricDataOutput output, CompletableFuture< | ||
| + | super(output, | ||
| + | } | ||
| - | private static class MyChestLootTables extends SimpleFabricLootTableProvider { | + | |
| - | | + | |
| - | super(dataGenerator, LootContextTypes.CHEST); | + | |
| - | } | + | |
| - | | + | |
| - | public void accept(BiConsumer< | + | public void accept(BiConsumer< |
| - | | + | |
| - | .pool(LootPool.builder().rolls(ConstantLootNumberProvider.create(1.0F)) | + | .pool(LootPool.builder().rolls(ConstantLootNumberProvider.create(1.0F)) |
| - | .with(ItemEntry.builder(Items.DIAMOND) | + | .with(ItemEntry.builder(Items.DIAMOND) |
| - | .apply(SetCountLootFunction.builder(ConstantLootNumberProvider.create(1.0F))) | + | .apply(SetCountLootFunction.builder(ConstantLootNumberProvider.create(1.0F)))) |
| - | .with(ItemEntry.builder(Items.DIAMOND_SWORD)).apply(EnchantWithLevelsLootFunction.create(UniformLootNumberProvider.create(20.0F, | + | .with(ItemEntry.builder(Items.DIAMOND_SWORD))) |
| - | ); | + | ); |
| - | } | + | } |
| } | } | ||
| + | </ | ||
| - | // ... | + | 然后在入口点中: |
| + | <code java ExampleModDataGenerator.java> | ||
| + | public class ExampleModDataGenerator implements DataGeneratorEntrypoint { | ||
| + | @Override | ||
| + | public void onInitializeDataGenerator(FabricDataGenerator generator) { | ||
| + | | ||
| - | @Override | + | pack.addProvider(TutorialChestLootTableProvider::new); |
| - | public void onInitializeDataGenerator(FabricDataGenerator fabricDataGenerator) { | + | } |
| - | fabricDataGenerator.addProvider(MyChestLootTables::new); | + | |
| } | } | ||
| </ | </ | ||
| + | |||
| + | > 如果使用的是 1.20 之前的版本,请将 '' | ||
| + | |||
| + | 现在运行数据生成器,然后启动 Minecraft,并运行多次命令 ''/ | ||
zh_cn/tutorial/datagen_loot.1676645893.txt.gz · Last modified: 2023/02/17 14:58 by hanatomizu