Skip to content

Commit 09a7aec

Browse files
authored
Vite Update, Turbo Update, fromString method, and Pipeline Update (#67)
* Vite Update, Turbo Update, fromString method, and Pipeline Update * Resolve feedback
1 parent bf22418 commit 09a7aec

File tree

10 files changed

+231
-87
lines changed

10 files changed

+231
-87
lines changed

.github/workflows/release-changes.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ on:
77

88
jobs:
99
build:
10+
if: github.actor != 'mc-npm'
1011
environment: release
1112
runs-on: ubuntu-latest
1213

@@ -21,11 +22,10 @@ jobs:
2122
- run: npm ci
2223
- run: npm run test # Fully build the repo so we have artifacts available to create releases, include tests so we don't ship in a broken state
2324

24-
# Uncomment the below when ready to publish to NPM for the first time
2525
- name: Set git credentials
2626
run: |
27-
git config --global user.name "Raphael Landaverde"
28-
git config --global user.email "rlanda@microsoft.com"
27+
git config --global user.name "Minecraft Bot"
28+
git config --global user.email "mc-npm@microsoft.com"
2929
git remote set-url origin "https://[email protected]/mojang/minecraft-scripting-libraries"
3030
env:
3131
REPO_PAT: ${{ secrets.REPO_PAT }}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "patch",
3+
"comment": "New fromString method using the same format as toString",
4+
"packageName": "@minecraft/math",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}

libraries/math/api-report/math.api.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ import type { Vector3 } from '@minecraft/server';
1010
// @public
1111
export function clampNumber(val: number, min: number, max: number): number;
1212

13+
// @public
14+
export const VECTOR2_ZERO: Vector2;
15+
1316
// @public
1417
export class Vector2Builder implements Vector2 {
18+
constructor(vecStr: string, delim?: string);
1519
constructor(vec: Vector2, arg?: never);
1620
constructor(x: number, y: number);
1721
// (undocumented)
@@ -27,6 +31,7 @@ export class Vector2Builder implements Vector2 {
2731

2832
// @public
2933
export class Vector2Utils {
34+
static fromString(str: string, delimiter?: string): Vector2 | undefined;
3035
static toString(v: Vector2, options?: {
3136
decimals?: number;
3237
delimiter?: string;
@@ -77,6 +82,7 @@ export const VECTOR3_ZERO: Vector3;
7782

7883
// @public
7984
export class Vector3Builder implements Vector3 {
85+
constructor(vecStr: string, delim?: string, arg2?: never);
8086
constructor(vec: Vector3, arg?: never, arg2?: never);
8187
constructor(x: number, y: number, z: number);
8288
add(v: Partial<Vector3>): this;
@@ -124,6 +130,7 @@ export class Vector3Utils {
124130
static dot(a: Vector3, b: Vector3): number;
125131
static equals(v1: Vector3, v2: Vector3): boolean;
126132
static floor(v: Vector3): Vector3;
133+
static fromString(str: string, delimiter?: string): Vector3 | undefined;
127134
static lerp(a: Vector3, b: Vector3, t: number): Vector3;
128135
static magnitude(v: Vector3): number;
129136
static multiply(a: Vector3, b: Vector3): Vector3;

libraries/math/src/vector3/coreHelpers.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,50 @@ describe('Vector3 operations', () => {
135135
expect(Vector2Utils.toString(vector, { delimiter: '|' })).toBe(expectedString4);
136136
});
137137

138+
describe('fromString', () => {
139+
it('creates a vector3 from a string', () => {
140+
const inputString = '1.00, 2.00, 3.00';
141+
const expectedVector: Vector3 = { x: 1, y: 2, z: 3 };
142+
expect(Vector3Utils.fromString(inputString)).toEqual(expectedVector);
143+
});
144+
145+
it('for a vector3 returns undefined for invalid formatting', () => {
146+
const invalidTooManyNumbers = '1.00, 2.00, 3.00, 345, 234234';
147+
const invalidIncorrectDelimiter = '1.00+ 2.00+ 3.00+ 345+ 234234';
148+
const invalidNonNumericElements = '1.00, wrong, 3.00, 3fgdf7, 234234';
149+
expect(Vector3Utils.fromString(invalidTooManyNumbers)).toBeUndefined();
150+
expect(Vector3Utils.fromString(invalidIncorrectDelimiter)).toBeUndefined();
151+
expect(Vector3Utils.fromString(invalidNonNumericElements)).toBeUndefined();
152+
});
153+
154+
it('creates a vector3 from a string with an overridden delimiter', () => {
155+
const inputString = '1.00+ 2.00+ 3.00';
156+
const expectedVector: Vector3 = { x: 1, y: 2, z: 3 };
157+
expect(Vector3Utils.fromString(inputString, '+')).toEqual(expectedVector);
158+
});
159+
160+
it('creates a vector2 from a string', () => {
161+
const inputString = '1.00, 2.00';
162+
const expectedVector: Vector2 = { x: 1, y: 2 };
163+
expect(Vector2Utils.fromString(inputString)).toEqual(expectedVector);
164+
});
165+
166+
it('for a vector2 returns undefined for invalid formatting', () => {
167+
const invalidTooManyNumbers = '1.00, 2.00, 3.00, 345, 234234';
168+
const invalidIncorrectDelimiter = '1.00+ 2.00';
169+
const invalidNonNumericElements = 'fgdf7, 234234';
170+
expect(Vector2Utils.fromString(invalidTooManyNumbers)).toBeUndefined();
171+
expect(Vector2Utils.fromString(invalidIncorrectDelimiter)).toBeUndefined();
172+
expect(Vector2Utils.fromString(invalidNonNumericElements)).toBeUndefined();
173+
});
174+
175+
it('creates a vector2 from a string with an overridden delimiter', () => {
176+
const inputString = '1.00+ 2.00';
177+
const expectedVector: Vector2 = { x: 1, y: 2 };
178+
expect(Vector2Utils.fromString(inputString, '+')).toEqual(expectedVector);
179+
});
180+
});
181+
138182
describe('clamp', () => {
139183
const v: Vector3 = { x: 1, y: 1, z: 3 };
140184
const minVec: Partial<Vector3> = { x: 0, y: 1.5 };

libraries/math/src/vector3/coreHelpers.ts

Lines changed: 57 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,7 @@ export class Vector3Utils {
6060
* Calculate the cross product of two vectors. Returns a new vector.
6161
*/
6262
static cross(a: Vector3, b: Vector3): Vector3 {
63-
return {
64-
x: a.y * b.z - a.z * b.y,
65-
y: a.z * b.x - a.x * b.z,
66-
z: a.x * b.y - a.y * b.x,
67-
};
63+
return { x: a.y * b.z - a.z * b.y, y: a.z * b.x - a.x * b.z, z: a.x * b.y - a.y * b.x };
6864
}
6965

7066
/**
@@ -115,18 +111,33 @@ export class Vector3Utils {
115111
return str.join(options?.delimiter ?? ', ');
116112
}
117113

114+
/**
115+
* fromString
116+
*
117+
* Gets a Vector3 from the string representation produced by {@link Vector3Utils.toString}. If any numeric value is not a number
118+
* or the format is invalid, undefined is returned.
119+
* @param str - The string to parse
120+
* @param delimiter - The delimiter used to separate the components. Defaults to the same as the default for {@link Vector3Utils.toString}
121+
*/
122+
static fromString(str: string, delimiter: string = ','): Vector3 | undefined {
123+
const parts = str.split(delimiter);
124+
if (parts.length !== 3) {
125+
return undefined;
126+
}
127+
128+
const output = parts.map(part => parseFloat(part));
129+
if (output.some(part => isNaN(part))) {
130+
return undefined;
131+
}
132+
return { x: output[0], y: output[1], z: output[2] };
133+
}
134+
118135
/**
119136
* clamp
120137
*
121138
* Clamps the components of a vector to limits to produce a new vector
122139
*/
123-
static clamp(
124-
v: Vector3,
125-
limits?: {
126-
min?: Partial<Vector3>;
127-
max?: Partial<Vector3>;
128-
}
129-
): Vector3 {
140+
static clamp(v: Vector3, limits?: { min?: Partial<Vector3>; max?: Partial<Vector3> }): Vector3 {
130141
return {
131142
x: clampNumber(v.x, limits?.min?.x ?? Number.MIN_SAFE_INTEGER, limits?.max?.x ?? Number.MAX_SAFE_INTEGER),
132143
y: clampNumber(v.y, limits?.min?.y ?? Number.MIN_SAFE_INTEGER, limits?.max?.y ?? Number.MAX_SAFE_INTEGER),
@@ -140,11 +151,7 @@ export class Vector3Utils {
140151
* Constructs a new vector using linear interpolation on each component from two vectors.
141152
*/
142153
static lerp(a: Vector3, b: Vector3, t: number): Vector3 {
143-
return {
144-
x: a.x + (b.x - a.x) * t,
145-
y: a.y + (b.y - a.y) * t,
146-
z: a.z + (b.z - a.z) * t,
147-
};
154+
return { x: a.x + (b.x - a.x) * t, y: a.y + (b.y - a.y) * t, z: a.z + (b.z - a.z) * t };
148155
}
149156

150157
/**
@@ -167,11 +174,7 @@ export class Vector3Utils {
167174
* Not to be confused with {@link Vector3Utils.dot} product or {@link Vector3Utils.cross} product
168175
*/
169176
static multiply(a: Vector3, b: Vector3): Vector3 {
170-
return {
171-
x: a.x * b.x,
172-
y: a.y * b.y,
173-
z: a.z * b.z,
174-
};
177+
return { x: a.x * b.x, y: a.y * b.y, z: a.z * b.z };
175178
}
176179

177180
/**
@@ -183,11 +186,7 @@ export class Vector3Utils {
183186
static rotateX(v: Vector3, a: number): Vector3 {
184187
let cos = Math.cos(a);
185188
let sin = Math.sin(a);
186-
return {
187-
x: v.x,
188-
y: v.y * cos - v.z * sin,
189-
z: v.z * cos + v.y * sin,
190-
};
189+
return { x: v.x, y: v.y * cos - v.z * sin, z: v.z * cos + v.y * sin };
191190
}
192191

193192
/**
@@ -199,11 +198,7 @@ export class Vector3Utils {
199198
static rotateY(v: Vector3, a: number): Vector3 {
200199
let cos = Math.cos(a);
201200
let sin = Math.sin(a);
202-
return {
203-
x: v.x * cos + v.z * sin,
204-
y: v.y,
205-
z: v.z * cos - v.x * sin,
206-
};
201+
return { x: v.x * cos + v.z * sin, y: v.y, z: v.z * cos - v.x * sin };
207202
}
208203

209204
/**
@@ -215,11 +210,7 @@ export class Vector3Utils {
215210
static rotateZ(v: Vector3, a: number): Vector3 {
216211
let cos = Math.cos(a);
217212
let sin = Math.sin(a);
218-
return {
219-
x: v.x * cos - v.y * sin,
220-
y: v.y * cos + v.x * sin,
221-
z: v.z,
222-
};
213+
return { x: v.x * cos - v.y * sin, y: v.y * cos + v.x * sin, z: v.z };
223214
}
224215
}
225216

@@ -239,6 +230,27 @@ export class Vector2Utils {
239230
const str: string[] = [v.x.toFixed(decimals), v.y.toFixed(decimals)];
240231
return str.join(options?.delimiter ?? ', ');
241232
}
233+
234+
/**
235+
* fromString
236+
*
237+
* Gets a Vector2 from the string representation produced by {@link Vector3Utils.toString}. If any numeric value is not a number
238+
* or the format is invalid, undefined is returned.
239+
* @param str - The string to parse
240+
* @param delimiter - The delimiter used to separate the components. Defaults to the same as the default for {@link Vector3Utils.toString}
241+
*/
242+
static fromString(str: string, delimiter: string = ','): Vector2 | undefined {
243+
const parts = str.split(delimiter);
244+
if (parts.length !== 2) {
245+
return undefined;
246+
}
247+
248+
const output = parts.map(part => parseFloat(part));
249+
if (output.some(part => isNaN(part))) {
250+
return undefined;
251+
}
252+
return { x: output[0], y: output[1] };
253+
}
242254
}
243255

244256
/**
@@ -357,3 +369,11 @@ export const VECTOR3_HALF: Vector3 = { x: 0.5, y: 0.5, z: 0.5 };
357369
* @public
358370
*/
359371
export const VECTOR3_NEGATIVE_ONE: Vector3 = { x: -1, y: -1, z: -1 };
372+
/**
373+
* zero
374+
*
375+
* A vector representing the value of 0 in all directions (0,0)
376+
*
377+
* @public
378+
*/
379+
export const VECTOR2_ZERO: Vector2 = { x: 0, y: 0 };

libraries/math/src/vector3/vectorWrapper.test.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import type { Vector3 } from '@minecraft/server';
55
import { describe, expect, it } from 'vitest';
6-
import { Vector2Utils, Vector3Utils } from './coreHelpers.js';
6+
import { VECTOR2_ZERO, Vector2Utils, VECTOR3_ZERO, Vector3Utils } from './coreHelpers.js';
77
import { Vector2Builder, Vector3Builder } from './vectorWrapper.js';
88

99
/**
@@ -181,6 +181,24 @@ describe('Vector3Builder', () => {
181181
expect(result).toEqual(vectorB);
182182
});
183183

184+
it('should be able to compute a string representation of the vector with the same result as the coreHelpers function using a string', () => {
185+
const regular = '1.33, 2.14, 3.55';
186+
const alternateDelimiter = '1.33+2.14+3.55';
187+
const vectorBuilderA = new Vector3Builder(regular);
188+
const vectorBuilderB = new Vector3Builder(alternateDelimiter, '+');
189+
190+
const vectorA = Vector3Utils.fromString(regular);
191+
const vectorB = Vector3Utils.fromString(alternateDelimiter, '+');
192+
193+
expect(vectorBuilderA).toEqual(vectorA);
194+
expect(vectorBuilderB).toEqual(vectorB);
195+
});
196+
197+
it('makes a 0 vector for invalid string', () => {
198+
const regular = '1.33, sdfg, 3.55';
199+
expect(new Vector3Builder(regular)).toEqual(VECTOR3_ZERO);
200+
});
201+
184202
it('should be able compute the lerp halfway between two vectors with the same result as the coreHelpers function', () => {
185203
const vectorA = new Vector3Builder(5, 6, 3);
186204
const vectorB = new Vector3Builder(4, 2, 6);
@@ -250,4 +268,22 @@ describe('Vector2Builder', () => {
250268
const result = vectorA.toString({ decimals: 1, delimiter: ' ' });
251269
expect(result).toEqual(vectorB);
252270
});
271+
272+
it('should be able to compute a string representation of the vector with the same result as the coreHelpers function using a string', () => {
273+
const regular = '1.33, 2.14';
274+
const alternateDelimiter = '1.33+2.14';
275+
const vectorBuilderA = new Vector2Builder(regular);
276+
const vectorBuilderB = new Vector2Builder(alternateDelimiter, '+');
277+
278+
const vectorA = Vector2Utils.fromString(regular);
279+
const vectorB = Vector2Utils.fromString(alternateDelimiter, '+');
280+
281+
expect(vectorBuilderA).toEqual(vectorA);
282+
expect(vectorBuilderB).toEqual(vectorB);
283+
});
284+
285+
it('makes a 0 vector for invalid string', () => {
286+
const regular = '1.33, sdfg';
287+
expect(new Vector2Builder(regular)).toEqual(VECTOR2_ZERO);
288+
});
253289
});

libraries/math/src/vector3/vectorWrapper.ts

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,29 @@ export class Vector3Builder implements Vector3 {
1919
y: number;
2020
z: number;
2121

22+
constructor(vecStr: string, delim?: string, arg2?: never);
2223
constructor(vec: Vector3, arg?: never, arg2?: never);
2324
constructor(x: number, y: number, z: number);
24-
constructor(first: number | Vector3, y?: number, z?: number) {
25+
constructor(first: number | Vector3 | string, second?: number | string, z?: number) {
2526
if (typeof first === 'object') {
2627
this.x = first.x;
2728
this.y = first.y;
2829
this.z = first.z;
30+
} else if (typeof first === 'string') {
31+
const parsed = Vector3Utils.fromString(first, (second as string | undefined) ?? ',');
32+
if (!parsed) {
33+
this.x = 0;
34+
this.y = 0;
35+
this.z = 0;
36+
37+
return;
38+
}
39+
this.x = parsed.x;
40+
this.y = parsed.y;
41+
this.z = parsed.z;
2942
} else {
3043
this.x = first;
31-
this.y = y ?? 0;
44+
this.y = (second as number) ?? 0;
3245
this.z = z ?? 0;
3346
}
3447
}
@@ -217,15 +230,27 @@ export class Vector2Builder implements Vector2 {
217230
x: number;
218231
y: number;
219232

233+
constructor(vecStr: string, delim?: string);
220234
constructor(vec: Vector2, arg?: never);
221235
constructor(x: number, y: number);
222-
constructor(first: number | Vector2, y?: number) {
236+
constructor(first: number | Vector2 | string, second?: number | string) {
223237
if (typeof first === 'object') {
224238
this.x = first.x;
225239
this.y = first.y;
240+
} else if (typeof first === 'string') {
241+
const parsed = Vector2Utils.fromString(first, (second as string | undefined) ?? ',');
242+
if (!parsed) {
243+
this.x = 0;
244+
this.y = 0;
245+
246+
return;
247+
}
248+
249+
this.x = parsed.x;
250+
this.y = parsed.y;
226251
} else {
227252
this.x = first;
228-
this.y = y ?? 0;
253+
this.y = (second as number) ?? 0;
229254
}
230255
}
231256

0 commit comments

Comments
 (0)