-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcs.js
More file actions
205 lines (190 loc) · 6.03 KB
/
cs.js
File metadata and controls
205 lines (190 loc) · 6.03 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
#!/usr/bin/env node
'use strict';
const playerdatadir = './playerdata';
const output = './output';
const blacklist = [
"minecraft:shulker_box",
"minecraft:white_shulker_box",
"minecraft:orange_shulker_box",
"minecraft:magenta_shulker_box",
"minecraft:light_blue_shulker_box",
"minecraft:yellow_shulker_box",
"minecraft:lime_shulker_box",
"minecraft:pink_shulker_box",
"minecraft:gray_shulker_box",
"minecraft:light_gray_shulker_box",
"minecraft:cyan_shulker_box",
"minecraft:purple_shulker_box",
"minecraft:blue_shulker_box",
"minecraft:brown_shulker_box",
"minecraft:green_shulker_box",
"minecraft:red_shulker_box",
"minecraft:black_shulker_box",
"minecraft:chest",
"minecraft:trapped_chest",
"minecraft:barrel",
"minecraft:axolotl_spawn_egg",
"minecraft:bat_spawn_egg",
"minecraft:bee_spawn_egg",
"minecraft:blaze_spawn_egg",
"minecraft:cave_spider_spawn_egg",
"minecraft:cat_spawn_egg",
"minecraft:chicken_spawn_egg",
"minecraft:cod_spawn_egg",
"minecraft:cow_spawn_egg",
"minecraft:creeper_spawn_egg",
"minecraft:dolphin_spawn_egg",
"minecraft:dolphin_spawn_egg",
"minecraft:dolphin_spawn_egg",
"minecraft:dolphin_spawn_egg",
"minecraft:enderman_spawn_egg",
"minecraft:endermite_spawn_egg",
"minecraft:endermite_spawn_egg",
"minecraft:fox_spawn_egg",
"minecraft:ghast_spawn_egg",
"minecraft:glow_squid_spawn_egg",
"minecraft:guardian_spawn_egg",
"minecraft:hoglin_spawn_egg",
"minecraft:horse_spawn_egg",
"minecraft:husk_spawn_egg",
"minecraft:llama_spawn_egg",
"minecraft:magma_cube_spawn_egg",
"minecraft:mooshroom_spawn_egg",
"minecraft:mule_spawn_egg",
"minecraft:ocelot_spawn_egg",
"minecraft:panda_spawn_egg",
"minecraft:parrot_spawn_egg",
"minecraft:phantom_spawn_egg",
"minecraft:pig_spawn_egg",
"minecraft:piglin_spawn_egg",
"minecraft:piglin_brute_spawn_egg",
"minecraft:pillager_spawn_egg",
"minecraft:polar_bear_spawn_egg",
"minecraft:pufferfish_spawn_egg",
"minecraft:rabbit_spawn_egg",
"minecraft:ravager_spawn_egg",
"minecraft:salmon_spawn_egg",
"minecraft:sheep_spawn_egg",
"minecraft:shulker_spawn_egg",
"minecraft:silverfish_spawn_egg",
"minecraft:skeleton_spawn_egg",
"minecraft:skeleton_horse_spawn_egg",
"minecraft:slime_spawn_egg",
"minecraft:spider_spawn_egg",
"minecraft:squid_spawn_egg",
"minecraft:stray_spawn_egg",
"minecraft:strider_spawn_egg",
"minecraft:trader_llama_spawn_egg",
"minecraft:tropical_fish_spawn_egg",
"minecraft:turtle_spawn_egg",
"minecraft:vex_spawn_egg",
"minecraft:villager_spawn_egg",
"minecraft:vindicator_spawn_egg",
"minecraft:wandering_trader_spawn_egg",
"minecraft:witch_spawn_egg",
"minecraft:wither_skeleton_spawn_egg",
"minecraft:wolf_spawn_egg",
"minecraft:zombie_spawn_egg",
"minecraft:zombie_horse_spawn_egg",
"minecraft:zombie_villager_spawn_egg",
"minecraft:zombified_piglin_spawn_egg",
"minecraft:spawner"
];
const whitelist = [
"minecraft:written_book"
];
const tagToRemove = [
"Enchantments",
"AttributeModifiers",
"HideFlags",
"Unbreakable",
"StoredEnchantments"
];
const attributesToRemove = [
"XpTotal",
"XpLevel",
"XpP"
];
const fs = require('fs/promises');
const path = require('path');
const gzip = require('node-gzip');
const nbt = require('prismarine-nbt');
let players = [];
let counter = 0;
(async () => {
const files = await fs.readdir(playerdatadir);
await Promise.all(
files.map(async (file) => {
if (file.split('.')[1] === 'dat' && file.length === 40) {
const playerfile = path.resolve(playerdatadir, file);
players.push(file);
}
})
);
for (let player of players) {
const buffer = await fs.readFile(path.resolve(playerdatadir, player));
const { parsed } = await nbt.parse(buffer);
if (parsed.value) {
const newNBT = await flatten(parsed);
console.log('Update', path.resolve(output, player));
counter += 1;
fs.writeFile(path.resolve(output, player), await gzip.gzip(await nbt.writeUncompressed(newNBT)));
}
}
console.log('Update complete with', counter, 'files written.')
})();
async function flatten(data) {
// attributes
attributesToRemove.forEach(function(attr) {
if (nestedValue(data, 'value', attr)) {
delete data.value[attr];
}
});
// inventory
if (data.value.Inventory) {
let inv = await shrink(data.value.Inventory.value.value);
data.value.Inventory.value.value = inv;
}
// ender items
if (data.value.EnderItems) {
let ender = await shrink(data.value.EnderItems.value.value);
data.value.EnderItems.value.value = ender;
}
return data;
}
async function shrink(items) {
let filtered = []
await Promise.all(items.map(async (item) => {
if (whitelist.indexOf(item.id.value) !== -1) {
// handle whitelist
item.Count.value = 1;
tagToRemove.forEach(function(tag) {
item = removeTag(item, tag);
});
filtered.push(item);
} else {
// filter blacklist
if (blacklist.indexOf(item.id.value) === -1) {
// remove stack
item.Count.value = 1;
if (nestedValue(item, 'tag', 'value', 'display', 'value', 'Lore')) {
tagToRemove.forEach(function(tag) {
item = removeTag(item, tag);
});
filtered.push(item);
}
}
}
}));
return filtered;
}
function removeTag(item, tag) {
if (nestedValue(item, 'tag', 'value', tag, 'type')) {
delete item.tag.value[tag];
}
return item;
}
// test & get value from nested object
function nestedValue(obj, ...args) {
return args.reduce((obj, level) => obj && obj[level], obj);
}