-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPlayerStatsTab.tsx
More file actions
280 lines (261 loc) · 11.7 KB
/
PlayerStatsTab.tsx
File metadata and controls
280 lines (261 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import { useMemo } from 'react';
import { Card, Row, Col, Slider, InputNumber, Select, Space, Typography, Divider, Tag } from 'antd';
import type { LoadedSave } from '../lib/containers';
import { TagType, nbt, cloneNbt, get } from '../lib/nbt';
import type { NbtList, NbtDouble } from '../lib/nbt';
const { Text } = Typography;
// pixel-art HUD icons from the game's loose PNGs
function HudIcon({ src, alt, scale = 2 }: { src: string; alt: string; scale?: number }) {
return (
<img src={src} alt={alt} title={alt}
style={{ imageRendering: 'pixelated', height: 18 * scale, width: 'auto', display: 'inline-block', verticalAlign: 'middle' }}
/>
);
}
interface Props {
loaded: LoadedSave;
onUpdate: (updater: (s: LoadedSave) => LoadedSave) => void;
}
export default function PlayerStatsTab({ loaded, onUpdate }: Props) {
const tags = loaded.playerNbt.root.tags;
function mutate(fn: (tags: Record<string, import('../lib/nbt').NbtValue>) => void) {
onUpdate(s => {
const clone = cloneNbt(s.playerNbt);
fn(clone.root.tags);
return { ...s, playerNbt: clone };
});
}
const health = useMemo(() => get.float(tags, 'Health') ?? 20, [tags]);
const food = useMemo(() => get.int(tags, 'FoodLevel') ?? 20, [tags]);
const foodSat = useMemo(() => get.float(tags, 'FoodSaturationLevel') ?? 5, [tags]);
const xpLevel = useMemo(() => get.int(tags, 'XpLevel') ?? 0, [tags]);
const xpP = useMemo(() => get.float(tags, 'XpP') ?? 0, [tags]);
const score = useMemo(() => get.int(tags, 'Score') ?? 0, [tags]);
const gameMode = useMemo(() => get.int(tags, 'playerGameType') ?? 0, [tags]);
// Pos is stored as a list of 3 doubles in the nbt
const posList = useMemo(() => {
const v = tags['Pos'];
if (v?.type !== TagType.List) return [0, 64, 0];
return (v as NbtList).items.map(d =>
d.type === TagType.Double ? (d as NbtDouble).value : 0
);
}, [tags]);
const spawnX = useMemo(() => get.int(tags, 'SpawnX') ?? 0, [tags]);
const spawnY = useMemo(() => get.int(tags, 'SpawnY') ?? 64, [tags]);
const spawnZ = useMemo(() => get.int(tags, 'SpawnZ') ?? 0, [tags]);
function setFloat(key: string, v: number) {
mutate(t => { t[key] = nbt.float(v); });
}
function setInt(key: string, v: number) {
mutate(t => { t[key] = nbt.int(v); });
}
function setPosCoord(idx: number, v: number) {
mutate(t => {
const list = t['Pos'] as NbtList | undefined;
if (!list || list.type !== TagType.List) {
t['Pos'] = { type: TagType.List, elementType: TagType.Double, items: [0, 64, 0].map(x => nbt.float(x)) };
}
const l = t['Pos'] as NbtList;
const items = [...l.items] as NbtDouble[];
items[idx] = { type: TagType.Double, value: v };
l.items = items;
});
}
const heartColor = (hp: number) => hp > 12 ? '#f87171' : hp > 6 ? '#facc15' : '#ef4444';
const foodColor = (f: number) => f > 14 ? '#a3e635' : f > 6 ? '#facc15' : '#f87171';
const cardStyle = {
background: '#161b22',
border: '1px solid #30363d',
borderRadius: 8,
};
return (
<Row gutter={[16, 16]}>
{/* ── health & food ─────────────────────────────────────────── */}
<Col span={24}>
<Card title={<span style={{ color: '#e6edf3', display: 'flex', alignItems: 'center', gap: 8 }}>
Vitals</span>} style={cardStyle} styles={{ header: { borderBottom: '1px solid #30363d' } }}>
<Row gutter={32}>
<Col xs={24} md={12}>
<StatRow
label="Health"
sublabel="(0 – 20 half-hearts)"
color={heartColor(health)}
value={health}
min={0} max={20} step={1}
onSlider={v => setFloat('Health', v)}
renderInput={() => (
<InputNumber
value={health} min={0} max={20} step={0.5}
size="small" style={{ width: 80 }}
onChange={v => v !== null && setFloat('Health', v)}
/>
)}
/>
</Col>
<Col xs={24} md={12}>
<StatRow
label="Food Level"
sublabel="(0 – 20)"
color={foodColor(food)}
value={food}
min={0} max={20}
onSlider={v => setInt('FoodLevel', v)}
renderInput={() => (
<InputNumber
value={food} min={0} max={20} size="small" style={{ width: 80 }}
onChange={v => v !== null && setInt('FoodLevel', v)}
/>
)}
/>
</Col>
<Col xs={24} md={12}>
<StatRow
label="Food Saturation"
sublabel="(0 – 5)"
color="#f59e0b"
value={foodSat}
min={0} max={5} step={0.5}
onSlider={v => setFloat('FoodSaturationLevel', v)}
renderInput={() => (
<InputNumber
value={foodSat} min={0} max={5} step={0.1} size="small" style={{ width: 80 }}
onChange={v => v !== null && setFloat('FoodSaturationLevel', v)}
/>
)}
/>
</Col>
</Row>
</Card>
</Col>
{/* ── xp ───────────────────────────────────────────────────── */}
<Col xs={24} md={12} style={{ display: 'flex', flexDirection: 'column' }}>
<Card title={<div style={{ display: 'flex', alignItems: 'center', gap: 8 }}><span style={{ color: '#e6edf3' }}>Experience</span></div>} style={{ ...cardStyle, flex: 1 }} styles={{ header: { borderBottom: '1px solid #30363d' } }}>
<Space direction="vertical" style={{ width: '100%' }} size={16}>
<div>
<div style={{ color: '#6e7681', fontSize: 12, marginBottom: 6 }}>XP Level</div>
<InputNumber
value={xpLevel} min={0} max={10000}
style={{ width: '100%' }}
addonBefore="Level"
onChange={v => v !== null && setInt('XpLevel', v)}
/>
</div>
<div>
<div style={{ color: '#6e7681', fontSize: 12, marginBottom: 6 }}>Progress within level (0 – 1)</div>
<Slider
value={xpP} min={0} max={1} step={0.01}
onChange={v => setFloat('XpP', v)}
trackStyle={{ background: '#a3e635' }}
/>
<InputNumber
value={xpP} min={0} max={1} step={0.01} size="small" style={{ width: 80 }}
onChange={v => v !== null && setFloat('XpP', v)}
/>
</div>
<div>
<div style={{ color: '#6e7681', fontSize: 12, marginBottom: 6 }}>Score</div>
<InputNumber
value={score} min={0} max={2147483647}
style={{ width: '100%' }}
onChange={v => v !== null && setInt('Score', v)}
/>
</div>
</Space>
</Card>
</Col>
{/* ── game mode ────────────────────────────────────────────── */}
<Col xs={24} md={12} style={{ display: 'flex', flexDirection: 'column' }}>
<Card title={<div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: 8 }}>
<span style={{ color: '#e6edf3' }}>Game Mode</span>
<Tag color={gameMode === 0 ? 'red' : gameMode === 1 ? 'blue' : 'orange'} style={{ fontSize: 13, padding: '4px 10px' }}>
{['Survival', 'Creative', 'Adventure'][gameMode] ?? 'Unknown'}
</Tag>
</div>} style={{ ...cardStyle, flex: 1 }} styles={{ header: { borderBottom: '1px solid #30363d' } }}>
<Select
value={gameMode}
style={{ width: '100%' }}
onChange={v => setInt('playerGameType', v)}
options={[
{ value: 0, label: 'Survival' },
{ value: 1, label: 'Creative' },
{ value: 2, label: 'Adventure' },
]}
/>
<Divider style={{ borderColor: '#30363d', margin: '16px 0' }} />
</Card>
</Col>
{/* ── position ─────────────────────────────────────────────── */}
<Col xs={24} md={12} style={{ display: 'flex', flexDirection: 'column' }}>
<Card title={<span style={{ color: '#e6edf3' }}>Position</span>} style={{ ...cardStyle, flex: 1 }} styles={{ header: { borderBottom: '1px solid #30363d' } }}>
<Space direction="vertical" style={{ width: '100%' }} size={10}>
{(['X', 'Y', 'Z'] as const).map((axis, i) => (
<div key={axis} className="flex items-center gap-3">
<Text style={{ width: 16, color: ['#f87171', '#4ade80', '#60a5fa'][i], fontWeight: 700, flexShrink: 0 }}>
{axis}
</Text>
<InputNumber
value={posList[i] ?? 0}
style={{ flex: 1 }}
step={1}
onChange={v => v !== null && setPosCoord(i, v)}
/>
</div>
))}
</Space>
</Card>
</Col>
{/* ── spawn point ──────────────────────────────────────────── */}
<Col xs={24} md={12} style={{ display: 'flex', flexDirection: 'column' }}>
<Card title={<span style={{ color: '#e6edf3' }}>Spawn Point</span>} style={{ ...cardStyle, flex: 1 }} styles={{ header: { borderBottom: '1px solid #30363d' } }}>
<Space direction="vertical" style={{ width: '100%' }} size={10}>
{[['SpawnX', '#f87171'], ['SpawnY', '#4ade80'], ['SpawnZ', '#60a5fa']] .map(([key, color]) => (
<div key={key} className="flex items-center gap-3">
<Text style={{ width: 56, color, flexShrink: 0, fontSize: 12, fontFamily: 'monospace' }}>
{key}
</Text>
<InputNumber
value={key === 'SpawnX' ? spawnX : key === 'SpawnY' ? spawnY : spawnZ}
style={{ flex: 1 }}
onChange={v => v !== null && setInt(key, v)}
/>
</div>
))}
</Space>
</Card>
</Col>
</Row>
);
}
// ── reusable stat row ────────────────────────────────────────────────────────
function StatRow({
label, sublabel, color, value, min, max, step = 1,
onSlider, renderInput,
}: {
label: string;
sublabel: string;
color: string;
value: number;
min: number;
max: number;
step?: number;
onSlider: (v: number) => void;
renderInput: () => React.ReactNode;
}) {
return (
<div className="mb-4">
<div className="flex justify-between mb-1">
<Text style={{ color: '#e6edf3', fontWeight: 600, fontSize: 13 }}>{label}</Text>
<Text style={{ color: '#6e7681', fontSize: 11 }}>{sublabel}</Text>
</div>
<div className="flex items-center gap-3">
<Slider
value={value} min={min} max={max} step={step}
onChange={onSlider}
style={{ flex: 1 }}
trackStyle={{ background: color }}
/>
{renderInput()}
</div>
</div>
);
}