我们本次在模组中实现一个自定义村民并与其进行交易
$$效果演示$$
1.1.19版本村民职业获取需要一个所谓的职业方块,所以我们要先准备一个方块给村民进行职业获取:
在BlockInit中增加一个职业方块:
BlockInit.java
public class BlockInit {
//职业方块
public static final Block STOVE = registerBlock("gashapon" ,
new StoveBlock(FabricBlockSettings.copy(Blocks.IRON_BLOCK).strength(1.5F, 4.0F).requiresTool().nonOpaque()),ModMain.ITEMTAB);
private static Block registerBlockWithoutBlockItem(String name, Block block) {
return Registry.register(Registry.BLOCK, new Identifier(ModMain.MOD_ID, name), block);
}
private static Block registerBlock(String name, Block block, ItemGroup group) {
registerBlockItem(name, block, group);
return Registry.register(Registry.BLOCK, new Identifier(ModMain.MOD_ID, name), block);
}
private static Item registerBlockItem(String name, Block block, ItemGroup group) {
return Registry.register(Registry.ITEM, new Identifier(ModMain.MOD_ID, name),
new BlockItem(block, new FabricItemSettings().group(group)));
}
public static void registerModBlocks() {
System.out.println("Registering Mod Blocks for " + ModMain.MOD_ID);
}
}
在资源包的data包中添加对该职业方块的tag标签:
acquirable_job_site.json
{
"values": [
"joyggd:gas_poi"
]
}
2.在Init包中新建一个VillagerInit类:
VillagerInit.java
package net.joy187.joyggd.init;
import com.google.common.collect.ImmutableSet;
import net.fabricmc.fabric.api.object.builder.v1.trade.TradeOfferHelper;
import net.fabricmc.fabric.api.object.builder.v1.villager.VillagerProfessionBuilder;
import net.fabricmc.fabric.api.object.builder.v1.world.poi.PointOfInterestHelper;
import net.joy187.joyggd.ModMain;
import net.minecraft.block.Block;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;
import net.minecraft.util.registry.RegistryKey;
import net.minecraft.village.TradeOffer;
import net.minecraft.village.VillagerProfession;
import net.minecraft.world.poi.PointOfInterestType;
public class VillagerInit {
//该村民对应的职业方块设定
public static final PointOfInterestType GAS_POI = registerPOI("gas_poi", BlockInit.STOVE);
//该村民的职业设定
public static final VillagerProfession GAS_MASTER = registerProfession("gasmaster",
RegistryKey.of(Registry.POINT_OF_INTEREST_TYPE_KEY, new Identifier(ModMain.MOD_ID, "gas_poi")));
public static VillagerProfession registerProfession(String name, RegistryKey<PointOfInterestType> type) {
return Registry.register(Registry.VILLAGER_PROFESSION, new Identifier(ModMain.MOD_ID, name),
VillagerProfessionBuilder.create().id(new Identifier(ModMain.MOD_ID, name)).workstation(type)
.workSound(SoundEvents.ENTITY_VILLAGER_WORK_ARMORER).build());
}
public static PointOfInterestType registerPOI(String name, Block block) {
return PointOfInterestHelper.register(new Identifier(ModMain.MOD_ID, name),
1, 1, ImmutableSet.copyOf(block.getStateManager().getStates()));
}
public static void registerVillagers() {
ModMain.LOGGER.debug("Registering Villagers for " + ModMain.MOD_ID);
}
//可以与该职业下的村民交易什么物品
public static void registerTrades() {
TradeOfferHelper.registerVillagerOffers(GAS_MASTER,1,
factories -> {
factories.add(((entity, random) -> new TradeOffer(
new ItemStack(Items.EMERALD, 20),
new ItemStack(BlockInit.STOVE2, 1),
6, 2, 0.02f
)));
});
TradeOfferHelper.registerVillagerOffers(GAS_MASTER,1,
factories -> {
factories.add(((entity, random) -> new TradeOffer(
new ItemStack(Items.EMERALD, 10),
new ItemStack(ItemInit.ASSASSIN, 1),
6, 2, 0.02f
)));
});
TradeOfferHelper.registerVillagerOffers(GAS_MASTER,2,
factories -> {
factories.add(((entity, random) -> new TradeOffer(
new ItemStack(Items.EMERALD, 15),
new ItemStack(ItemInit.DRESS, 1),
6, 20, 0.02f
)));
});
}
}
交易参数说明
registerVillagerOffers(职业,交易等级,交易详情factories)
factories.add(拿什么换,换到了什么,最大交易次数,交易一次得到的经验奖励,激怒村民后涨价幅度)
在项目主类中添加VillagerInit的注册:
ModMain.java
public class ModMain implements ModInitializer {
public static final String MOD_ID = "joyggd";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
public static final ItemGroup ITEMTAB = FabricItemGroupBuilder.build(
new Identifier(MOD_ID, "itemtab"), () -> new ItemStack(ItemInit.SHERIFF));
@Override
public void onInitialize() {
ModConfigs.registerConfigs();
ItemInit.registerModItems();
BlockInit.registerModBlocks();
EffectInit.registerEffects();
SoundInit.registerSounds();
BlockEntityInit.registerAllBlockEntities();
//添加这两个事件的注册
VillagerInit.registerVillagers();
VillagerInit.registerTrades();
}
}
3.在资源包中添加职业名称:
en_us.json
"entity.minecraft.villager.gasmaster": "Gashapon Man",
在textures\entity\villager\profession
中添加该职业村民的皮肤:
$Orz$
tql