-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWorldTab.tsx
More file actions
306 lines (289 loc) · 13.4 KB
/
WorldTab.tsx
File metadata and controls
306 lines (289 loc) · 13.4 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import { useMemo } from 'react';
import { Card, Row, Col, Slider, InputNumber, Select, Switch, Space, Typography, Divider, Tag } from 'antd';
import type { LoadedSave } from '../lib/containers';
import { TagType, nbt, cloneNbt, get } from '../lib/nbt';
import type { NbtCompound, NbtString } from '../lib/nbt';
const { Text, Title } = Typography;
interface Props {
loaded: LoadedSave;
onUpdate: (updater: (s: LoadedSave) => LoadedSave) => void;
}
export default function WorldTab({ loaded, onUpdate }: Props) {
if (!loaded.levelNbt) {
return (
<div style={{ color: '#6e7681', textAlign: 'center', padding: 40 }}>
No level.dat found in this save container.
</div>
);
}
// level.dat root is sometimes wrapped in a "Data" compound — handle both layouts
const rootTags = loaded.levelNbt.root.tags;
const dataTags: Record<string, import('../lib/nbt').NbtValue> =
rootTags['Data']?.type === TagType.Compound
? (rootTags['Data'] as NbtCompound).tags
: rootTags;
function mutate(fn: (data: Record<string, import('../lib/nbt').NbtValue>) => void) {
onUpdate(s => {
if (!s.levelNbt) return s;
const clone = cloneNbt(s.levelNbt);
const root = clone.root.tags;
const data = root['Data']?.type === TagType.Compound
? (root['Data'] as NbtCompound).tags
: root;
fn(data);
return { ...s, levelNbt: clone };
});
}
const levelName = useMemo(() => get.str(dataTags, 'LevelName') ?? '', [dataTags]);
const seed = useMemo(() => get.long(dataTags, 'RandomSeed') ?? 0n, [dataTags]);
const dayTime = useMemo(() => Number(get.long(dataTags, 'DayTime') ?? 0n) % 24000, [dataTags]);
const raining = useMemo(() => !!(get.byte(dataTags, 'raining') ?? 0), [dataTags]);
const thundering= useMemo(() => !!(get.byte(dataTags, 'thundering') ?? 0), [dataTags]);
const difficulty= useMemo(() => get.byte(dataTags, 'Difficulty') ?? get.int(dataTags, 'Difficulty') ?? 1, [dataTags]);
const gameType = useMemo(() => get.int(dataTags, 'GameType') ?? 0, [dataTags]);
const gameRules = useMemo(() => {
const gr = dataTags['GameRules'];
if (gr?.type !== TagType.Compound) return {} as Record<string, string>;
return Object.fromEntries(
Object.entries((gr as NbtCompound).tags)
.filter(([, v]) => v.type === TagType.String)
.map(([k, v]) => [k, (v as NbtString).value])
);
}, [dataTags]);
function setStr(key: string, v: string) { mutate(d => { d[key] = nbt.str(v); }); }
function setInt(key: string, v: number) { mutate(d => { d[key] = nbt.int(v); }); }
function setByte(key: string, v: number) { mutate(d => { d[key] = nbt.byte(v); }); }
function setLong(key: string, v: bigint) { mutate(d => { d[key] = nbt.long(v); }); }
function setDayTime(v: number) {
mutate(d => { d['DayTime'] = nbt.long(BigInt(v)); });
}
function setGameRule(rule: string, enabled: boolean) {
mutate(d => {
let gr = d['GameRules'];
if (!gr || gr.type !== TagType.Compound) {
gr = nbt.compound({});
d['GameRules'] = gr;
}
(gr as NbtCompound).tags[rule] = nbt.str(enabled ? 'true' : 'false');
});
}
const timeOfDay = (t: number) => {
if (t < 1000 || t >= 23000) return 'Dawn';
if (t < 6000) return 'Morning';
if (t < 12000) return 'Afternoon';
if (t < 13000) return 'Dusk';
return 'Night';
};
const cardStyle = {
background: '#161b22',
border: '1px solid #30363d',
borderRadius: 8,
};
const GAME_RULES: Array<{ key: string; label: string; desc: string }> = [
{ key: 'keepInventory', label: 'Keep Inventory', desc: 'Items kept on death' },
{ key: 'doMobSpawning', label: 'Mob Spawning', desc: 'Hostile/passive mobs spawn' },
{ key: 'mobGriefing', label: 'Mob Griefing', desc: 'Creepers & endermen can modify terrain' },
{ key: 'doFireTick', label: 'Fire Spreading', desc: 'Fire spreads to adjacent blocks' },
{ key: 'doDaylightCycle', label: 'Daylight Cycle', desc: 'Time of day advances' },
{ key: 'doMobLoot', label: 'Mob Loot', desc: 'Mobs drop items on death' },
{ key: 'doTileDrops', label: 'Block Drops', desc: 'Blocks drop items when broken' },
{ key: 'naturalRegeneration', label: 'Natural Regen', desc: 'Health regenerates when food is full' },
];
return (
<Row gutter={[16, 16]}>
{/* ── world info ─────────────────────────────────────────── */}
<Col span={24}>
<Card title={<span style={{ color: '#e6edf3' }}>World Info</span>} style={cardStyle} styles={{ header: { borderBottom: '1px solid #30363d' } }}>
<Row gutter={24}>
<Col xs={24} md={12}>
<div style={{ marginBottom: 12 }}>
<div style={{ color: '#6e7681', fontSize: 12, marginBottom: 6 }}>World Name</div>
<input
value={levelName}
onChange={e => setStr('LevelName', e.target.value)}
style={{
width: '100%', background: '#0d1117', border: '1px solid #30363d',
borderRadius: 6, padding: '6px 10px', color: '#e6edf3', fontSize: 14,
outline: 'none',
}}
/>
</div>
</Col>
<Col xs={24} md={12}>
<div>
<div style={{ color: '#6e7681', fontSize: 12, marginBottom: 6 }}>Seed</div>
<input
value={seed.toString()}
onChange={e => {
try { setLong('RandomSeed', BigInt(e.target.value)); } catch { /* ignore non-numeric input */ }
}}
style={{
width: '100%', background: '#0d1117', border: '1px solid #30363d',
borderRadius: 6, padding: '6px 10px', color: '#4ade80',
fontFamily: 'monospace', fontSize: 13, outline: 'none',
}}
/>
</div>
</Col>
</Row>
</Card>
</Col>
{/* ── time & weather ────────────────────────────────────────── */}
<Col xs={24} md={12} style={{ display: 'flex', flexDirection: 'column' }}>
<Card title={<span style={{ color: '#e6edf3' }}>Time & Weather</span>} style={{ ...cardStyle, flex: 1 }} styles={{ header: { borderBottom: '1px solid #30363d' } }}>
<Space direction="vertical" style={{ width: '100%' }} size={16}>
<div>
<div className="flex justify-between mb-2">
<Text style={{ color: '#6e7681', fontSize: 12 }}>Time of Day (0 – 24000)</Text>
<Tag color="blue">{timeOfDay(dayTime)}</Tag>
</div>
<Slider
value={dayTime} min={0} max={23999}
onChange={setDayTime}
trackStyle={{ background: dayTime < 13000 ? '#facc15' : '#3b82f6' }}
/>
<div className="flex gap-2 flex-wrap">
{[['Dawn', 0], ['Noon', 6000], ['Midnight', 18000]].map(([label, val]) => (
<button
key={String(val)}
onClick={() => setDayTime(Number(val))}
style={{
background: '#0d1117', border: '1px solid #30363d',
color: '#e6edf3', borderRadius: 4, padding: '2px 8px',
cursor: 'pointer', fontSize: 12,
}}
>
{label}
</button>
))}
</div>
</div>
<Divider style={{ borderColor: '#30363d', margin: '4px 0' }} />
<div className="flex justify-between items-center">
<Text style={{ color: '#e6edf3' }}>Raining</Text>
<Switch
checked={raining}
onChange={v => setByte('raining', v ? 1 : 0)}
style={{ background: raining ? '#60a5fa' : undefined }}
/>
</div>
<div className="flex justify-between items-center">
<Text style={{ color: '#e6edf3' }}>Thunderstorm</Text>
<Switch
checked={thundering}
onChange={v => { setByte('thundering', v ? 1 : 0); if (v) setByte('raining', 1); }}
style={{ background: thundering ? '#a78bfa' : undefined }}
/>
</div>
</Space>
</Card>
</Col>
{/* ── difficulty & game mode ───────────────────────────────── */}
<Col xs={24} md={12} style={{ display: 'flex', flexDirection: 'column' }}>
<Card title={<span style={{ color: '#e6edf3' }}>Difficulty & Mode</span>} 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: 8 }}>Difficulty</div>
<Select
value={difficulty}
style={{ width: '100%' }}
onChange={v => setByte('Difficulty', v)}
options={[
{ value: 0, label: 'Peaceful' },
{ value: 1, label: 'Easy' },
{ value: 2, label: 'Normal' },
{ value: 3, label: 'Hard' },
]}
/>
</div>
<div>
<div style={{ color: '#6e7681', fontSize: 12, marginBottom: 8 }}>Default Game Mode</div>
<Select
value={gameType}
style={{ width: '100%' }}
onChange={v => setInt('GameType', v)}
options={[
{ value: 0, label: 'Survival' },
{ value: 1, label: 'Creative' },
{ value: 2, label: 'Adventure' },
]}
/>
</div>
</Space>
</Card>
</Col>
{/* ── game rules ───────────────────────────────────────────── */}
<Col span={24}>
<Card title={<span style={{ color: '#e6edf3' }}>Game Rules</span>} style={cardStyle} styles={{ header: { borderBottom: '1px solid #30363d' } }}>
{Object.keys(gameRules).length === 0 ? (
<Text style={{ color: '#484f58' }}>No game rules found in level.dat.</Text>
) : (
<Row gutter={[12, 12]}>
{GAME_RULES.map(({ key, label, desc }) => {
const val = gameRules[key];
if (val === undefined) return null;
const enabled = val === 'true';
return (
<Col xs={24} sm={12} md={8} key={key}>
<div
style={{
background: '#0d1117',
border: `1px solid ${enabled ? '#4ade8033' : '#30363d'}`,
borderRadius: 6,
padding: '10px 12px',
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
gap: 8,
}}
>
<div>
<div style={{ color: '#e6edf3', fontSize: 13, fontWeight: 500 }}>{label}</div>
<div style={{ color: '#484f58', fontSize: 11 }}>{desc}</div>
</div>
<Switch
size="small"
checked={enabled}
onChange={v => setGameRule(key, v)}
style={{ flexShrink: 0, background: enabled ? '#4ade80' : undefined }}
/>
</div>
</Col>
);
})}
{/* unknown game rules found in the save — show them as plain toggles */}
{Object.entries(gameRules)
.filter(([k]) => !GAME_RULES.some(r => r.key === k))
.map(([key, val]) => {
const enabled = val === 'true';
return (
<Col xs={24} sm={12} md={8} key={key}>
<div
style={{
background: '#0d1117',
border: `1px solid ${enabled ? '#4ade8033' : '#30363d'}`,
borderRadius: 6,
padding: '10px 12px',
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
}}
>
<Text style={{ color: '#e6edf3', fontSize: 12, fontFamily: 'monospace' }}>{key}</Text>
<Switch
size="small"
checked={enabled}
onChange={v => setGameRule(key, v)}
style={{ background: enabled ? '#4ade80' : undefined }}
/>
</div>
</Col>
);
})}
</Row>
)}
</Card>
</Col>
</Row>
);
}