Skip to content

Commit 6db67e2

Browse files
committed
Initial implementation of a @minecraft/math library with support for
two paradigms. Added test and lint support, and also migrated lint rule for command suggestions to this repo. Started the creation of shared build tools.
1 parent 134670d commit 6db67e2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+9026
-2
lines changed

.github/CODEOWNERS

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* @rlandav @JakeShirley

.github/workflows/beachball-check.yml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: Beachball Changefile Check
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- main
7+
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- uses: actions/checkout@v4
14+
- name: Use Node.js 20.x
15+
uses: actions/setup-node@v3
16+
with:
17+
node-version: '20.x'
18+
- run: npx beachball check

.github/workflows/pull-request.yml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: Pull Request Validation
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- main
7+
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- uses: actions/checkout@v4
14+
- name: Use Node.js 20.x
15+
uses: actions/setup-node@v3
16+
with:
17+
node-version: '20.x'
18+
- run: npm ci
19+
- run: npm run test

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.turbo
2+
lib
3+
dist
4+
temp
5+
node_modules

.npmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
engine-strict=true

CODE_OF_CONDUCT.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Microsoft Open Source Code of Conduct
2+
3+
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).
4+
5+
Resources:
6+
7+
- [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/)
8+
- [Microsoft Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/)
9+
- Contact [[email protected]](mailto:[email protected]) with questions or concerns

CONTRIBUTING.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Contributing
2+
3+
This project welcomes contributions and suggestions. Most contributions require you to
4+
agree to a Contributor License Agreement (CLA) declaring that you have the right to,
5+
and actually do, grant us the rights to use your contribution. For details, visit
6+
https://cla.microsoft.com.
7+
8+
When you submit a pull request, a CLA-bot will automatically determine whether you need
9+
to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the
10+
instructions provided by the bot. You will only need to do this once across all repositories using our CLA.
11+
12+
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).
13+
For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/)
14+
or contact [[email protected]](mailto:[email protected]) with any additional questions or comments.
15+
16+
## Specific Guidelines
17+
18+
Contributions to these libraries are welcome! These libraries are maintained by Mojang but we are open to contributions and bug fixes. In general, we consider the backwards compatibility and versioning to be the absolute top priority when making changes to the libraries. Because of this, we strictly adhere to semver for changes, and libraries depend on minecraft modules at specific major versions as well. Any PR submitted to any library **must** contain a change file using the beachball tool to indicate the severity of the change. When a change is submitted, it will automatically update versions and publish to NPM via our pipelines.

LICENSE renamed to LICENSE.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2023 Mojang
3+
Copyright (c) 2023 Mojang AB
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

+48-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,49 @@
11
# minecraft-scripting-libraries
2-
Sets of typescript scripting libraries for use with the minecraft scripting modules.
2+
3+
This repository contains a set of scripting libraries for use with the minecraft scripting modules in creating content for the game. The repository is structured as a javascript "monorepo" that contains multiple packages. These packages are fully javascript and are typically intended to server as helper libraries that can be commonly used when creating content. The libraries are provided both through NPM, but also in pre-bundled forms in cases where user workflows do not leverage techniques such as bundling for their own content creation.
4+
5+
More details on each individual library are in the README files for individual packages. The packages are located generally within the libraries subfolder.
6+
7+
# Working in the repository
8+
9+
## Prerequisites
10+
11+
1. Install the latest LTS of [Node](https://nodejs.org/en/download) (20 or higher) and confirm after installation that you have NPM 10 or higher also installed.
12+
1. It is recommended to use [nvm](https://github.com/nvm-sh/nvm) or [nvm-windows](https://github.com/coreybutler/nvm-windows) for ease of management of node version installation.
13+
1. Globally install the [turbo tool](https://turbo.build/repo/docs/installing) using the command `npm install --global turbo`. The turbo tool is used under the covers for all of the build scripts, and having it accessible globally is convenient for running builds from subdirectories as well.
14+
15+
## Install
16+
17+
Run
18+
19+
```ts
20+
npm install
21+
```
22+
23+
from the root of this repository to install all appropriate dependencies.
24+
25+
## Build
26+
27+
To build all packages, run
28+
29+
```
30+
npm run build
31+
```
32+
33+
This will build all packages in this repository in the right order using `turbo`. This is equivalent to running `turbo build`. If you would like to build or work on a specific package, perform a full build first, and then navigate to the package you care about and from there you can then run `npm run build` to build that specific package. Each individual package has it's own README and potentially additional scripts. Refer to the readme per package for more information.
34+
35+
## Linting
36+
37+
All packages are validated via ESLint with a consistent set of rules as well as enforces styling through prettier. To explicitly run linting, use `npm run lint`, and some changes can be fixed automatically with `npm run lint -- --fix`.
38+
39+
## Testing
40+
41+
All packages support testing via `vitest`. Tests are authored with the `.test.ts` suffix and tests can be run with `npm run test`.
42+
43+
## Contributing
44+
45+
See [Contributing.md](./CONTRIBUTING.md) for details on contributions
46+
47+
## Trademark Notice
48+
49+
Trademarks This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft trademarks or logos is subject to and must follow Microsoft’s Trademark & Brand Guidelines. Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship. Any use of third-party trademarks or logos are subject to those third-party’s policies.

SECURITY.md

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<!-- BEGIN MICROSOFT SECURITY.MD V0.0.9 BLOCK -->
2+
3+
## Security
4+
5+
Microsoft takes the security of our software products and services seriously, which includes all source code repositories managed through our GitHub organizations, which include [Microsoft](https://github.com/Microsoft), [Azure](https://github.com/Azure), [DotNet](https://github.com/dotnet), [AspNet](https://github.com/aspnet) and [Xamarin](https://github.com/xamarin).
6+
7+
If you believe you have found a security vulnerability in any Microsoft-owned repository that meets [Microsoft's definition of a security vulnerability](https://aka.ms/security.md/definition), please report it to us as described below.
8+
9+
## Reporting Security Issues
10+
11+
**Please do not report security vulnerabilities through public GitHub issues.**
12+
13+
Instead, please report them to the Microsoft Security Response Center (MSRC) at [https://msrc.microsoft.com/create-report](https://aka.ms/security.md/msrc/create-report).
14+
15+
If you prefer to submit without logging in, send email to [[email protected]](mailto:[email protected]). If possible, encrypt your message with our PGP key; please download it from the [Microsoft Security Response Center PGP Key page](https://aka.ms/security.md/msrc/pgp).
16+
17+
You should receive a response within 24 hours. If for some reason you do not, please follow up via email to ensure we received your original message. Additional information can be found at [microsoft.com/msrc](https://www.microsoft.com/msrc).
18+
19+
Please include the requested information listed below (as much as you can provide) to help us better understand the nature and scope of the possible issue:
20+
21+
* Type of issue (e.g. buffer overflow, SQL injection, cross-site scripting, etc.)
22+
* Full paths of source file(s) related to the manifestation of the issue
23+
* The location of the affected source code (tag/branch/commit or direct URL)
24+
* Any special configuration required to reproduce the issue
25+
* Step-by-step instructions to reproduce the issue
26+
* Proof-of-concept or exploit code (if possible)
27+
* Impact of the issue, including how an attacker might exploit the issue
28+
29+
This information will help us triage your report more quickly.
30+
31+
If you are reporting for a bug bounty, more complete reports can contribute to a higher bounty award. Please visit our [Microsoft Bug Bounty Program](https://aka.ms/security.md/msrc/bounty) page for more details about our active programs.
32+
33+
## Preferred Languages
34+
35+
We prefer all communications to be in English.
36+
37+
## Policy
38+
39+
Microsoft follows the principle of [Coordinated Vulnerability Disclosure](https://aka.ms/security.md/cvd).
40+
41+
<!-- END MICROSOFT SECURITY.MD BLOCK -->

SUPPORT.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Support
2+
3+
## How to file issues and get help
4+
5+
This project uses GitHub Issues to track bugs and feature requests. Please search the existing
6+
issues before filing new issues to avoid duplicates. For new issues, file your bug or
7+
feature request as a new Issue.
8+
9+
## Microsoft Support Policy
10+
11+
Support for this repository and libraries is limited to the resources listed above.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "major",
3+
"comment": "Initial implementation of a @minecraft/math library with support for two paradigms. Added test and lint support, and also migrated lint rule for command suggestions to this repo. Started the creation of shared build tools.",
4+
"packageName": "@minecraft/core-build-tasks",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "major",
3+
"comment": "Initial implementation of a @minecraft/math library with support for two paradigms. Added test and lint support, and also migrated lint rule for command suggestions to this repo. Started the creation of shared build tools.",
4+
"packageName": "@minecraft/math",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "major",
3+
"comment": "Initial implementation of a @minecraft/math library with support for two paradigms. Added test and lint support, and also migrated lint rule for command suggestions to this repo. Started the creation of shared build tools.",
4+
"packageName": "eslint-plugin-minecraft-linting",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}

libraries/math/.eslintrc.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
root: true,
3+
// This tells ESLint to load the config from the package `eslint-config-minecraft-scripting`
4+
extends: ['minecraft-scripting'],
5+
parserOptions: {
6+
tsconfigRootDir: __dirname,
7+
},
8+
};

libraries/math/README.md

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Minecraft Math
2+
3+
A set of utilities and functions for common math operations. Major pieces are covered below.
4+
5+
## Vector3
6+
7+
A set of free functions and a wrapper class for common vector3 operations. Two distinct patterns are supported, a more pure computational approach operating on the Vector3 interface with no mutation, and a separate wrapper object oriented approach following a "builder" pattern. It is mostly preference whether you prefer the more "mutation" heavy pattern or the functional pattern, so it depends on the structure of your code. Under the covers, the same helpers are used.
8+
9+
### Pure Functional Style
10+
11+
```ts
12+
import { Vector3, world } from '@minecraft/server';
13+
import { MinecraftDimensionTypes } from '@minecraft/vanilla-data';
14+
import { add, subtract, cross } from '@minecraft/math';
15+
16+
const vectorA: Vector3 = {x: 1, y: 2, z:3};
17+
const vectorB: Vector3 = {x: 4, y: 5, z:6};
18+
19+
const resultAdd = add(vectorA, vectorB); // {x:5, y:7, z:9}
20+
const resultSubtract = subtract(vectorA, vectorB); // {x:-3, y:-3, z:-3}
21+
const resultAdd = cross(vectorA, vectorB); // {x:-3, y:6, z:-3}
22+
23+
console.log(toString(vectorA)); // Prints out "1, 2, 3"
24+
25+
// Use your vectors with any @minecraft/server API
26+
const = dimension = world.getDimension(MinecraftDimensionTypes.Overworld);
27+
dimension.spawnParticle("minecraft:colored_flame_particle", resultAdd);
28+
```
29+
30+
### Builder Style
31+
32+
```ts
33+
import { Vector3, world } from '@minecraft/server';
34+
import { Vector3Builder } from '@minecraft/math';
35+
import { MinecraftDimensionTypes } from '@minecraft/vanilla-data';
36+
37+
const vectorA: Vector3Builder = new Vector3Builder({x: 1, y: 2, z:3});
38+
const vectorB: Vector3 = {x: 4, y: 5, z:6};
39+
const vectorC: Vector3 = {x: 1, y: 3, z:5};
40+
41+
// Mutates vectorA directly each time
42+
vectorA.add(vectorB).subtract(vectorC).cross(vectorB); // Final result {x:4, y:-8, z:4}
43+
44+
console.log(vectorA.toString()); // Prints out "4, -8, 4"
45+
46+
// Vector3Builder type can directly be used in APIs that accept Vector3
47+
const dimension = world.getDimension(MinecraftDimensionTypes.Overworld);
48+
dimension.spawnParticle("minecraft:colored_flame_particle", vectorA);
49+
```
50+
51+
## How to use @minecraft/math in your project
52+
53+
@minecraft/math is published to NPM and follows standard semver semantics. To use it in your project, there are two main options:
54+
55+
1. Download `@minecraft/math` from NPM by doing `npm install @minecraft/math` within your scripts pack. By using `@minecraft/math`, you will need to do some sort of bundling to merge the library into your packs code. We recommend using [esbuild](https://esbuild.github.io/getting-started/#your-first-bundle) for simplicity.
56+
2. This repository publishes releases for `@minecraft/math`, and on each release we attach a pre-bundled copy of the `@minecraft/math` module. Feel free to take this JS bundle and integrate into your projects as it contains all dependencies coupled together, and this pattern does not require bundling.

libraries/math/api-extractor.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* Config file for API Extractor. For more info, please visit: https://api-extractor.com
3+
*/
4+
{
5+
"$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json",
6+
"extends": "@minecraft/api-extractor-base/api-extractor-base.json"
7+
}

0 commit comments

Comments
 (0)