Skip to content

Commit 29c6528

Browse files
committed
refactor: make settings overridable
1 parent 840b489 commit 29c6528

2 files changed

Lines changed: 19 additions & 9 deletions

File tree

src/lib/settings.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import { stringify } from 'lib/util'
66
import { createLogger } from 'lib/utils/logger'
77
import { WeakPlayerMap } from 'lib/weak-player-storage'
88
import { MemoryTable, Table, table } from './database/abstract'
9+
import { Message } from './i18n/message'
910
import { i18n, noI18n } from './i18n/text'
1011
import stringifyError from './utils/error'
11-
import { Message } from './i18n/message'
1212

1313
// TODO refactor(leaftail1880): Move all types under the Settings namespace
1414
// TODO refactor(leaftail1880): Move everything into the lib/settings/ folder
@@ -99,6 +99,11 @@ export class Settings {
9999
fn.groupId = groupId
100100
fn.groupName = groupName
101101
fn.extend = [groupName, groupId] as const
102+
fn.override = (setting: keyof Config, value: Partial<SettingsConfig<PlayerSettingValues>[string]>) => {
103+
for (const [k, v] of Object.entries(value)) {
104+
if (config[setting]) (config[setting] as unknown as Record<string, unknown>)[k] = v
105+
}
106+
}
102107

103108
return fn
104109
}
Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import { system } from '@minecraft/server'
2-
import { actionGuard, ActionGuardOrder, Menu, Settings } from 'lib'
32
import { ActionbarPriority } from 'lib/extensions/on-screen-display'
43
import { i18n } from 'lib/i18n/text'
4+
import { actionGuard, ActionGuardOrder } from 'lib/region'
5+
import { Menu } from 'lib/rpg/menu'
6+
import { Settings } from 'lib/settings'
57

68
const playerSettings = Settings.player(...Menu.settings, {
79
breakItemDurabilityNotify: {
810
name: i18n`Уведомлять о ломающемся предмете`,
911
description: i18n`Если включено, то когда вы взаимодействуете с блоком предметом, у которого остается менее 90%% прочности, показывать уведомление на весь экран`,
1012
value: true,
1113
},
12-
// TODO Donut feature
1314
breakItemDurabilityCancel: {
1415
name: i18n`Не давать ломать предмет`,
1516
description: i18n`Если включено, то когда вы взаимодействуете с блоком предметом, у которого остается менее 99%% прочности, взаимодействие будет отменено, а предмет - спасен.`,
@@ -18,21 +19,21 @@ const playerSettings = Settings.player(...Menu.settings, {
1819
})
1920

2021
actionGuard((player, _, ctx) => {
21-
const settings = playerSettings(player)
22-
if (!settings.breakItemDurabilityNotify) return
2322
if (ctx.type !== 'break') return
2423

2524
const durability = ctx.event.itemStack?.durability
2625
if (!durability) return
2726

2827
const { damage, maxDurability } = durability
28+
const percent = 100 - (damage / maxDurability) * 100
2929

30-
const percent = (damage / maxDurability) * 100
31-
32-
if (percent >= 90)
30+
const settings = playerSettings(player)
31+
if (percent < 10 && settings.breakItemDurabilityNotify)
3332
system.delay(() => {
3433
player.onScreenDisplay.setActionBar(
35-
i18n.error`Инструмент скоро сломается! ${damage}/${maxDurability} (${percent}%)`.to(player.lang),
34+
i18n.error`Инструмент скоро сломается! ${maxDurability - damage}/${maxDurability} (${percent}%)`.to(
35+
player.lang,
36+
),
3637
ActionbarPriority.Highest,
3738
)
3839
})
@@ -41,3 +42,7 @@ actionGuard((player, _, ctx) => {
4142
return false
4243
}
4344
}, ActionGuardOrder.Feature)
45+
46+
export const breakItemDurabilityNotify = {
47+
playerSettings,
48+
}

0 commit comments

Comments
 (0)