MerchantJS
本模组是 KubeJS 的附加组件,旨在扩展其在交易系统方面的功能。
在任何地方创建自定义交易
本模组允许你在任何地方发起交易——与猪交易、与末地石交易、与附魔金苹果交易,甚至凭空交易!
// server_scripts
ItemEvents.entityInteracted(event => {
// 检查被交互的生物是否为猪
if (event.getTarget().type === "minecraft:pig") {
// 创建一个完整的交易报价
let offer0 = MerchantJSUtils.createMerchantOffer({
buy: Item.of("minecraft:potato").toNBT(),
buyB: Item.of("minecraft:bread").toNBT(),
sell: Item.of("minecraft:stone").toNBT(),
uses: 0,
maxUses: 50,
xp: 5,
priceMultiplier: 0,
demand: 0
});
// 创建一个简单的交易报价
let offer1 = MerchantJSUtils.createMerchantOffer({
buy: Item.of("minecraft:beef").toNBT(),
sell: Item.of("minecraft:egg").toNBT()
});
// 为指定玩家打开带有给定标题和报价的自定义交易 GUI
MerchantJSUtils.openMerchant(
event.player,
Component.translatable("Little Piggy’s Cartoon House"),
[offer0, offer1]
);
}
});
上述示例允许玩家与猪进行交易,并拥有一个完全可定制的交易 GUI!
如果你需要更深入的自定义——请继续阅读。
选择性交易可用性
让我们先解释每个参数的含义:
let offer0 = MerchantJSUtils.createMerchantOffer({
buy: Item.of("minecraft:potato").toNBT(), // 输入物品 A(必填)
buyB: Item.of("minecraft:bread").toNBT(), // 输入物品 B(可选,默认为空)
sell: Item.of("minecraft:stone").toNBT(), // 输出物品(必填)
uses: 0, // 已使用次数(可选,默认 0)
maxUses: 50, // 最大使用次数(可选,默认 99)
xp: 5, // 经验奖励(可选,默认 0)
priceMultiplier: 0, // 价格波动乘数(可选,默认 0)
demand: 0 // 初始需求值(可选,默认 0)
});
将 maxUses 设置为 0 将使该交易显示为不可用。
现在将其与此事件结合以进行动态交易控制:
// client_scripts
MerchantEvents.switchTrade(event => {
if (event.titleKey == "Little Piggy’s Cartoon House") {
// 检查这是否是猪的交易窗口(也适用于原版交易!)
if (event.getOffer().getMaxUses() == 0) {
Client.player.tell("此交易已锁定...");
}
}
});
通过这种方式,你可以选择性地启用或禁用交易,这对于分阶段游戏或进度系统非常有用。
原版交易兼容性
原版交易已经具备许多机制。
MerchantJS 主要专注于轻松创建额外的交易。
对于补货、经验、需求或价格波动等功能——你需要自行处理。
至少,你需要一个地方来存储与交易相关的数据(如库存或存货信息),
因为每次调用 openMerchant 都会重新定义报价。
幸运的是,KubeJS 的 persistentData 非常适合于此用途。
剩下的就是根据需要触发数据刷新。
// server_scripts
MerchantEvents.afterTrade(event => {
event.player.tell(`感谢您购买 ${event.getOffer().result.id}!欢迎下次光临!`);
event.player.addXP(event.getOffer().xp);
});
此事件在发生任何交易后触发——也适用于原版交易!
如果你想在进行交易后触发其他操作,就在这里实现。
至于补货——只需在每次打开交易窗口时检查时间间隔即可。
未来计划
目前暂无计划——但欢迎提出你的愿望!
MerchantJS
This mod is an addon for KubeJS, expanding its functionality in the trading system.
Create Custom Trades Anywhere
This mod allows you to initiate trades anywhere — trade with pigs, with End Stone, with Enchanted Golden Apples, or even out of thin air!
// server_scripts
ItemEvents.entityInteracted(event => {
// Check if the interacted entity is a pig
if (event.getTarget().type === "minecraft:pig") {
// Create a complete trade offer
let offer0 = MerchantJSUtils.createMerchantOffer({
buy: Item.of("minecraft:potato").toNBT(),
buyB: Item.of("minecraft:bread").toNBT(),
sell: Item.of("minecraft:stone").toNBT(),
uses: 0,
maxUses: 50,
xp: 5,
priceMultiplier: 0,
demand: 0
});
// Create a simple trade offer
let offer1 = MerchantJSUtils.createMerchantOffer({
buy: Item.of("minecraft:beef").toNBT(),
sell: Item.of("minecraft:egg").toNBT()
});
// Open a custom trading GUI with the given player, title, and offers
MerchantJSUtils.openMerchant(
event.player,
Component.translatable("Little Piggy’s Cartoon House"),
[offer0, offer1]
);
}
});
The above example lets players trade with pigs, with a fully customizable trading GUI!
And if you need deeper customization — read on.
Selective Trade Availability
Let’s first explain the meaning of each parameter:
let offer0 = MerchantJSUtils.createMerchantOffer({
buy: Item.of("minecraft:potato").toNBT(), // Input item A (required)
buyB: Item.of("minecraft:bread").toNBT(), // Input item B (optional, defaults to empty)
sell: Item.of("minecraft:stone").toNBT(), // Output item (required)
uses: 0, // Number of times used (optional, default 0)
maxUses: 50, // Maximum uses (optional, default 99)
xp: 5, // Experience reward (optional, default 0)
priceMultiplier: 0, // Price fluctuation multiplier (optional, default 0)
demand: 0 // Initial demand value (optional, default 0)
});
Setting maxUses to 0 will make the trade appear unavailable.
Now combine it with this event for dynamic trade control:
// client_scripts
MerchantEvents.switchTrade(event => {
if (event.titleKey == "Little Piggy’s Cartoon House") {
// Check if it’s the pig’s trading window (works with vanilla ones too!)
if (event.getOffer().getMaxUses() == 0) {
Client.player.tell("This trade is locked...");
}
}
});
With this, you can selectively enable or disable trades, which is perfect for stage-based gameplay or progression systems.
Vanilla Trade Compatibility
Vanilla trading already has many mechanics.
MerchantJS mainly focuses on creating additional trades easily.
For features like restocking, experience, demand, or price fluctuation — you’ll need to handle those yourself.
At minimum, you’ll need a place to store trade-related data (like inventory or stock info),
since each call to openMerchant defines the offers anew.
Fortunately, KubeJS’s persistentData works great for this.
Then, all that’s left is triggering data refresh when needed.
// server_scripts
MerchantEvents.afterTrade(event => {
event.player.tell(`Thank you for purchasing ${event.getOffer().result.id}! Come again soon!`);
event.player.addXP(event.getOffer().xp);
});
This event fires after any trade occurs — and works for vanilla trades too!
If you want to trigger additional actions after trading, this is the place to do it.
As for restocking — just check the time interval whenever you open the trade window.
Future Plans
None for now — but feel free to make a wish!
评论 0
登录后参与评分
还没有评论,来抢沙发吧