跳到主要内容

5物品合成村民交易

自定义熔炉和合成表:

public class CustomRecipe {
public static void register() {
/**
* 熔炉合成
*/
NamespacedKey key1 = new NamespacedKey(MyPlugin.instance, "furnace_recipe");
// 钻石
ItemStack result1 = new ItemStack(Material.DIAMOND, 1);
FurnaceRecipe furnaceRecipe = new FurnaceRecipe(key1, result1, Material.DIRT, 0f, 3 * 20);
Bukkit.addRecipe(furnaceRecipe);

/**
* 自定义工作台合成
* Shaped 定型
* Shapeless 无定型
* rows
* [" P"," S"," "]
*/
NamespacedKey craftKey = new NamespacedKey(MyPlugin.instance, "craft_table");
ShapedRecipe shapedRecipe = new ShapedRecipe(craftKey, result1);
// 摆放合成表
shapedRecipe.shape(
" P",
" S",
" ");
// 设置合成表的原料
shapedRecipe.setIngredient('P', Material.BLAZE_POWDER);
shapedRecipe.setIngredient('S', Material.SLIME_BALL);
Bukkit.addRecipe(shapedRecipe);
}
}

自定义一个生成村民的指令:

    villager:
description: 生成一只特殊的村民

具体操作:

/**
* @author houyunfei
*/
public class VillagerExecutor implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if ("villager".equalsIgnoreCase(label)) {
if (sender instanceof Player player) {
World world = player.getWorld();
Location location = player.getLocation();

Villager villager = (Villager) world.spawnEntity(location, EntityType.VILLAGER);
// 修改村民信息
villager.setAI(false);
villager.setCustomName("蔡徐坤的奸商村民");

// 交易集合
List<MerchantRecipe> recipes = new ArrayList<>();
// 结果1
ItemStack result1 = new ItemStack(Material.DIAMOND_BLOCK, 1);
// 交易1
MerchantRecipe recipe1 = new MerchantRecipe(result1, 3);
// 交易所需物品
List<ItemStack> materials1 = new ArrayList<>();
materials1.add(new ItemStack(Material.DIRT, 3));
recipe1.setIngredients(materials1);
recipe1.setIngredients(materials1);
recipes.add(recipe1);


// 结果2
ItemStack result2 = new ItemStack(Material.FEATHER, 2);
MerchantRecipe recipe2 = new MerchantRecipe(result2, 1);
List<ItemStack> materials2 = new ArrayList<>();
materials2.add(new ItemStack(Material.DIRT, 1));
materials2.add(new ItemStack(Material.DIAMOND_BLOCK, 2));
recipe2.setIngredients(materials2);
recipes.add(recipe2);

// 设置交易
villager.setRecipes(recipes);

return true;
}
}
return false;
}
}

效果:

合成表:

image-20240909223010838

熔炉:

image-20240909223233088

村民交易:

image-20240909223511329