本篇所有内容基于1.20.1的KubeJS
作用:世界生成的战利品箱现在需要开锁工具才能打开,但是对于箱子矿车的战利品箱无效
战利品箱上锁机制 - 详细说明:
每个战利品箱生成时会随机一个"锁值"(lockValue),数值越大锁越难开
玩家需要用开锁工具(铁/钻石/下界合金剑)来开锁
每次开锁尝试会减少锁值,减少的幅度取决于钥匙品质
当锁值降到"成功阈值"(successThreshold)以下时,开锁成功
注:钥匙品质越高,开锁效率越高,耐久消耗越低
以下是全部代码
需要放置到server_scripts
BlockEvents.rightClicked(event => {
//判断是否是战利品箱
const isLoot = event.block.entityData?.LootTable
if (isLoot) {
let chestData = event.block.getEntity().persistentData
let lockValue = chestData.getInt('Lock')
const lockPick = event.player.getMainHandItem()
// 锁初始化,为未上锁的箱子设置随机锁值
if (!lockValue) {
//如果容器没有锁值,则随机生成数值,数值越大表示锁越难开
lockValue = Math.floor(Math.random() * 100 + 50) // 初始锁值范围
chestData.putInt('Lock', lockValue)
}
// 开锁流程,仅在箱子未解锁时执行
if (lockValue != 1) {
// 检查玩家是否持有正确的开锁工具
let unlockValue, successThreshold, durabilityCost
// 根据钥匙类型设置开锁参数
if (lockPick.id == 'minecraft:iron_sword') {
unlockValue = Math.floor(Math.random() * 10 + 1) //铁开锁工具:每次减1-10
successThreshold = 10 // 需减到≤10
durabilityCost = 3 // 每次尝试耐久的消耗
} else if (lockPick.id == 'minecraft:diamond_sword') {
unlockValue = Math.floor(Math.random() * 15 + 5) // 钻石开锁工具:每次减5-15
successThreshold = 5 // 需减到≤5
durabilityCost = 2 // 每次尝试耐久的消耗
} else if (lockPick.id == 'minecraft:netherite_sword') {
unlockValue = Math.floor(Math.random() * 20 + 10) // 下界开锁工具:每次减10-20
successThreshold = 3 //需减到≤3
durabilityCost = 1 // 每次尝试耐久的消耗
}
// 无钥匙/错误工具处理
if (lockValue > 1 && !['minecraft:iron_sword', 'minecraft:diamond_sword', 'minecraft:netherite_sword'].includes(lockPick.id)) {
event.player.setStatusMessage('§d这个容器已上锁,你需要一把§a[开锁工具]§d才能打开它')
event.level.playSound(null, event.block.pos.x, event.block.pos.y, event.block.pos.z,
'minecraft:block.iron_trapdoor.close', 2.0, 1.2, 'players')
event.player.playSound('minecraft:block.chain.break', 1.0, 0.8)
event.cancel()
}
// 有效开锁尝试处理
else if (['minecraft:iron_sword', 'minecraft:diamond_sword', 'minecraft:netherite_sword'].includes(lockPick.id)) {
// 开锁成功
if (lockValue - unlockValue <= successThreshold) {
chestData.putInt('Lock', 1) // 完全解锁
event.player.setStatusMessage("§6开锁成功!")
event.player.damageHeldItem('main_hand', durabilityCost) // 消耗耐久
event.level.playSound(null, event.block.pos.x, event.block.pos.y, event.block.pos.z,
'minecraft:block.note_block.bell', 2.0, 1.2, 'players')
return // 允许打开容器
}
// 开锁失败
else {
event.player.setStatusMessage("§c开锁失败!")
lockValue = lockValue - unlockValue // 降低锁值
event.player.damageHeldItem('main_hand', durabilityCost) // 消耗耐久
chestData.putInt('Lock', lockValue) // 更新锁值
event.player.playSound('minecraft:block.iron_trapdoor.open', 1.0, 0.8)
event.cancel() // 阻止打开容器
}
}
}
}
})