aboutsummaryrefslogtreecommitdiff
path: root/src/lib/common/util/Minecraft.ts
blob: a12ebf2b18d259f49b3617e2cad50be90db35807 (plain)
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import { Byte, Int, parse } from '@ironm00n/nbt-ts';
import { BitField } from 'discord.js';
import path from 'path';
import { fileURLToPath } from 'url';

const __dirname = path.dirname(fileURLToPath(import.meta.url));

export enum FormattingCodes {
	Black = '§0',
	DarkBlue = '§1',
	DarkGreen = '§2',
	DarkAqua = '§3',
	DarkRed = '§4',
	DarkPurple = '§5',
	Gold = '§6',
	Gray = '§7',
	DarkGray = '§8',
	Blue = '§9',
	Green = '§a',
	Aqua = '§b',
	Red = '§c',
	LightPurple = '§d',
	Yellow = '§e',
	White = '§f',

	Obfuscated = '§k',
	Bold = '§l',
	Strikethrough = '§m',
	Underline = '§n',
	Italic = '§o',
	Reset = '§r'
}

// https://minecraft.fandom.com/wiki/Formatting_codes
export const formattingInfo = {
	[FormattingCodes.Black]: {
		foreground: 'rgb(0, 0, 0)',
		foregroundDarker: 'rgb(0, 0, 0)',
		background: 'rgb(0, 0, 0)',
		backgroundDarker: 'rgb(0, 0, 0)',
		ansi: '\u001b[0;30m'
	},
	[FormattingCodes.DarkBlue]: {
		foreground: 'rgb(0, 0, 170)',
		foregroundDarker: 'rgb(0, 0, 118)',
		background: 'rgb(0, 0, 42)',
		backgroundDarker: 'rgb(0, 0, 29)',
		ansi: '\u001b[0;34m'
	},
	[FormattingCodes.DarkGreen]: {
		foreground: 'rgb(0, 170, 0)',
		foregroundDarker: 'rgb(0, 118, 0)',
		background: 'rgb(0, 42, 0)',
		backgroundDarker: 'rgb(0, 29, 0)',
		ansi: '\u001b[0;32m'
	},
	[FormattingCodes.DarkAqua]: {
		foreground: 'rgb(0, 170, 170)',
		foregroundDarker: 'rgb(0, 118, 118)',
		background: 'rgb(0, 42, 42)',
		backgroundDarker: 'rgb(0, 29, 29)',
		ansi: '\u001b[0;36m'
	},
	[FormattingCodes.DarkRed]: {
		foreground: 'rgb(170, 0, 0)',
		foregroundDarker: 'rgb(118, 0, 0)',
		background: 'rgb(42, 0, 0)',
		backgroundDarker: 'rgb(29, 0, 0)',
		ansi: '\u001b[0;31m'
	},
	[FormattingCodes.DarkPurple]: {
		foreground: 'rgb(170, 0, 170)',
		foregroundDarker: 'rgb(118, 0, 118)',
		background: 'rgb(42, 0, 42)',
		backgroundDarker: 'rgb(29, 0, 29)',
		ansi: '\u001b[0;35m'
	},
	[FormattingCodes.Gold]: {
		foreground: 'rgb(255, 170, 0)',
		foregroundDarker: 'rgb(178, 118, 0)',
		background: 'rgb(42, 42, 0)',
		backgroundDarker: 'rgb(29, 29, 0)',
		ansi: '\u001b[0;33m'
	},
	[FormattingCodes.Gray]: {
		foreground: 'rgb(170, 170, 170)',
		foregroundDarker: 'rgb(118, 118, 118)',
		background: 'rgb(42, 42, 42)',
		backgroundDarker: 'rgb(29, 29, 29)',
		ansi: '\u001b[0;37m'
	},
	[FormattingCodes.DarkGray]: {
		foreground: 'rgb(85, 85, 85)',
		foregroundDarker: 'rgb(59, 59, 59)',
		background: 'rgb(21, 21, 21)',
		backgroundDarker: 'rgb(14, 14, 14)',
		ansi: '\u001b[0;90m'
	},
	[FormattingCodes.Blue]: {
		foreground: 'rgb(85, 85, 255)',
		foregroundDarker: 'rgb(59, 59, 178)',
		background: 'rgb(21, 21, 63)',
		backgroundDarker: 'rgb(14, 14, 44)',
		ansi: '\u001b[0;94m'
	},
	[FormattingCodes.Green]: {
		foreground: 'rgb(85, 255, 85)',
		foregroundDarker: 'rgb(59, 178, 59)',
		background: 'rgb(21, 63, 21)',
		backgroundDarker: 'rgb(14, 44, 14)',
		ansi: '\u001b[0;92m'
	},
	[FormattingCodes.Aqua]: {
		foreground: 'rgb(85, 255, 255)',
		foregroundDarker: 'rgb(59, 178, 178)',
		background: 'rgb(21, 63, 63)',
		backgroundDarker: 'rgb(14, 44, 44)',
		ansi: '\u001b[0;96m'
	},
	[FormattingCodes.Red]: {
		foreground: 'rgb(255, 85, 85)',
		foregroundDarker: 'rgb(178, 59, 59)',
		background: 'rgb(63, 21, 21)',
		backgroundDarker: 'rgb(44, 14, 14)',
		ansi: '\u001b[0;91m'
	},
	[FormattingCodes.LightPurple]: {
		foreground: 'rgb(255, 85, 255)',
		foregroundDarker: 'rgb(178, 59, 178)',
		background: 'rgb(63, 21, 63)',
		backgroundDarker: 'rgb(44, 14, 44)',
		ansi: '\u001b[0;95m'
	},
	[FormattingCodes.Yellow]: {
		foreground: 'rgb(255, 255, 85)',
		foregroundDarker: 'rgb(178, 178, 59)',
		background: 'rgb(63, 63, 21)',
		backgroundDarker: 'rgb(44, 44, 14)',
		ansi: '\u001b[0;93m'
	},
	[FormattingCodes.White]: {
		foreground: 'rgb(255, 255, 255)',
		foregroundDarker: 'rgb(178, 178, 178)',
		background: 'rgb(63, 63, 63)',
		backgroundDarker: 'rgb(44, 44, 44)',
		ansi: '\u001b[0;97m'
	},

	[FormattingCodes.Obfuscated]: { ansi: '\u001b[8m' },
	[FormattingCodes.Bold]: { ansi: '\u001b[1m' },
	[FormattingCodes.Strikethrough]: { ansi: '\u001b[9m' },
	[FormattingCodes.Underline]: { ansi: '\u001b[4m' },
	[FormattingCodes.Italic]: { ansi: '\u001b[3m' },
	[FormattingCodes.Reset]: { ansi: '\u001b[0m' }
} as const;

export type McItemId = Lowercase<string>;
export type SbItemId = Uppercase<string>;
export type MojangJson = string;
export type SbRecipeItem = `${SbItemId}:${number}` | '';
export type SbRecipe = {
	[Location in `${'A' | 'B' | 'C'}${1 | 2 | 3}`]: SbRecipeItem;
};
export type InfoType = 'WIKI_URL' | '';

export type Slayer = `${'WOLF' | 'BLAZE' | 'EMAN'}_${1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9}`;

export interface RawNeuItem {
	itemid: McItemId;
	displayname: string;
	nbttag: MojangJson;
	damage: number;
	lore: string[];
	recipe?: SbRecipe;
	internalname: SbItemId;
	modver: string;
	infoType: InfoType;
	info?: string[];
	crafttext: string;
	vanilla?: boolean;
	useneucraft?: boolean;
	slayer_req?: Slayer;
	clickcommand?: string;
	x?: number;
	y?: number;
	z?: number;
	island?: string;
	recipes?: { type: string; cost: any[]; result: SbItemId }[];
	/** @deprecated */
	parent?: SbItemId;
	noseal?: boolean;
}

export enum HideFlagsBits {
	Enchantments = 1,
	AttributeModifiers = 2,
	Unbreakable = 4,
	CanDestroy = 8,
	CanPlaceOn = 16,
	/**
	 * potion effects, shield pattern info, "StoredEnchantments", written book
	 * "generation" and "author", "Explosion", "Fireworks", and map tooltips
	 */
	OtherInformation = 32,
	Dyed = 64
}

export type HideFlagsString = keyof typeof HideFlagsBits;

export class HideFlags extends BitField<HideFlagsString> {
	public static override Flags = HideFlagsBits;
}

export const formattingCode = new RegExp(
	`§[${Object.values(FormattingCodes)
		.filter((v) => v.startsWith('§'))
		.map((v) => v.substring(1))
		.join('')}]`
);

export function removeMCFormatting(str: string) {
	return str.replaceAll(formattingCode, '');
}

const repo = path.join(__dirname, '..', '..', '..', '..', '..', 'neu-item-repo-dangerous');

export interface NbtTag {
	overrideMeta?: Byte;
	Unbreakable?: Int;
	ench?: string[];
	HideFlags?: HideFlags;
	SkullOwner?: SkullOwner;
	display?: NbtTagDisplay;
	ExtraAttributes?: ExtraAttributes;
}

export interface SkullOwner {
	Id?: string;
	Properties?: {
		textures?: { Value?: string }[];
	};
}

export interface NbtTagDisplay {
	Lore?: string[];
	color?: Int;
	Name?: string;
}

export type RuneId = string;

export interface ExtraAttributes {
	originTag?: Origin;
	id?: string;
	generator_tier?: Int;
	boss_tier?: Int;
	enchantments?: { hardened_mana?: Int };
	dungeon_item_level?: Int;
	runes?: { [key: RuneId]: Int };
	petInfo?: PetInfo;
}

export interface PetInfo {
	type: 'ZOMBIE';
	active: boolean;
	exp: number;
	tier: 'COMMON' | 'UNCOMMON' | 'RARE' | 'EPIC' | 'LEGENDARY';
	hideInfo: boolean;
	candyUsed: number;
}

export type Origin = 'SHOP_PURCHASE';

const neuConstantsPath = path.join(repo, 'constants');
const neuPetsPath = path.join(neuConstantsPath, 'pets.json');
const neuPets = (await import(neuPetsPath, { assert: { type: 'json' } })) as PetsConstants;
const neuPetNumsPath = path.join(neuConstantsPath, 'petnums.json');
const neuPetNums = (await import(neuPetNumsPath, { assert: { type: 'json' } })) as PetNums;

export interface PetsConstants {
	pet_rarity_offset: Record<string, number>;
	pet_levels: number[];
	custom_pet_leveling: Record<string, { type: number; pet_levels: number[]; max_level: number }>;
	pet_types: Record<string, string>;
}

export interface PetNums {
	[key: string]: {
		[key: string]: {
			'1': {
				otherNums: number[];
				statNums: Record<string, number>;
			};
			'100': {
				otherNums: number[];
				statNums: Record<string, number>;
			};
			'stats_levelling_curve'?: `${number};${number};${number}`;
		};
	};
}

export class NeuItem {
	public itemId: McItemId;
	public displayName: string;
	public nbtTag: NbtTag;
	public internalName: SbItemId;
	public lore: string[];

	public constructor(raw: RawNeuItem) {
		this.itemId = raw.itemid;
		this.nbtTag = <NbtTag>parse(raw.nbttag);
		this.displayName = raw.displayname;
		this.internalName = raw.internalname;
		this.lore = raw.lore;

		this.petLoreReplacements();
	}

	private petLoreReplacements(level = -1) {
		if (/.*?;[0-5]$/.test(this.internalName) && this.displayName.includes('LVL')) {
			const maxLevel = neuPets?.custom_pet_leveling?.[this.internalName]?.max_level ?? 100;
			this.displayName = this.displayName.replace('LVL', `1➡${maxLevel}`);

			const nums = neuPetNums[this.internalName];
			if (!nums) throw new Error(`Pet (${this.internalName}) has no pet nums.`);

			const teir = ['COMMON', 'UNCOMMON', 'RARE', 'EPIC', 'LEGENDARY', 'MYTHIC'][+this.internalName.at(-1)!];
			const petInfoTier = nums[teir];
			if (!petInfoTier) throw new Error(`Pet (${this.internalName}) has no pet nums for ${teir} rarity.`);

			const curve = petInfoTier?.stats_levelling_curve?.split(';');

			// todo: finish copying from neu

			const minStatsLevel = parseInt(curve?.[0] ?? '0');
			const maxStatsLevel = parseInt(curve?.[0] ?? '100');

			const lore = '';
		}
	}
}

export function mcToAnsi(str: string) {
	for (const format in formattingInfo) {
		str = str.replaceAll(format, formattingInfo[format as keyof typeof formattingInfo].ansi);
	}
	return `${str}\u001b[0m`;
}