Skip to content

Commit

Permalink
Resource packs working!
Browse files Browse the repository at this point in the history
  • Loading branch information
MulverineX committed Mar 6, 2023
1 parent 2c4cc86 commit a8046a7
Show file tree
Hide file tree
Showing 43 changed files with 856 additions and 175 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ You don't need to remember commands syntax anymore.
This autocompletion works for all resources: commands, predicates, loot tables, advancements...

## 📂 Better organisation of resources
You can have multiple functions, advancements, loot tables per files - or you can keep the vanilla organisation, and have only 1 per file. Sandstone allows you to organise your data pack as you prefer, without sticking to Mojang's conventions.
You can have multiple functions, advancements, loot tables per files - or you can keep the vanilla organisation, and have only 1 per file. Sandstone allows you to organise your datapack as you prefer, without sticking to Mojang's conventions.

You also benefit from all the capabilities of a real programming language: multiline comments, indentation, documentation...

Expand Down
47 changes: 35 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "sandstone",
"description": "Sandstone, a Typescript library for Minecraft datapacks.",
"version": "0.14.0-alpha.19",
"version": "0.14.0-alpha.20",
"contributors": [
{
"name": "TheMrZZ - Florian ERNST",
Expand All @@ -17,9 +17,12 @@
"url": "https://github.com/sandstone-mc/sandstone/issues"
},
"dependencies": {
"@sinclair/typebox": "^0.25.24",
"@smithed-mc/data-types": "^0.1.2",
"@types/adm-zip": "^0.5.0",
"@types/fs-extra": "^11.0.1",
"@types/lodash": "^4.14.191",
"@types/semver": "^7.3.13",
"@typescript/analyze-trace": "^0.4.0",
"adm-zip": "^0.5.10",
"chalk": "^4",
Expand Down
2 changes: 1 addition & 1 deletion src/arguments/resources/datapack/criteria/ItemCriterion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ export type ItemCriterion = Partial<{
/** A brewed potion ID. */
potion: string

/** An item data pack tag. */
/** An item datapack tag. */
tag: string | TagClass<'items'>
}>
2 changes: 1 addition & 1 deletion src/arguments/resources/datapack/recipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ type RecipeKind<NAME extends string, VALUES extends Record<string, unknown> | un
* - `crafting_shape`: Represents a shaped crafting recipe in a crafting table.
* - `crafting_shapeless`: Represents a shapeless crafting recipe in a crafting table.
* - `crafting_special_*`: Represents a crafting recipe in a crafting table that is handled with builtin logic instead of being data-driven.
* When the "vanilla" data pack is disabled, they can be used to reenable desired builtin crafting recipes.
* When the "vanilla" datapack is disabled, they can be used to reenable desired builtin crafting recipes.
* - `smelting`: Represents a recipe in a furnace.
* - `smithing`: Represents a recipe in a smithing table.
* - `smoking`: Represents a recipe in a smoker.
Expand Down
2 changes: 1 addition & 1 deletion src/arguments/resources/datapack/structure.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { LiteralUnion } from 'sandstone/utils'
import type { BLOCKS, RootNBT } from '../index'
import type { BLOCKS, RootNBT } from '../../index'

type Vec3 = [number, number, number]

Expand Down
6 changes: 3 additions & 3 deletions src/arguments/resources/datapack/trimMaterial.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { LiteralUnion } from 'sandstone/utils'
import type { JSONTextComponent } from '../jsonTextComponent'
import type { ITEMS } from '#arguments/generated'
import type { ITEMS } from '../../generated'
import type { JSONTextComponent } from '../../jsonTextComponent'
import type { LiteralUnion } from '#utils'

/** Key is armor material, value is a string which will be used in the resource pack. */
export type OverrideArmorMaterials = Record<LiteralUnion<'leather' | 'chainmail' | 'iron' | 'gold' | 'diamond' | 'turtle' | 'netherite'>, string>
Expand Down
6 changes: 3 additions & 3 deletions src/arguments/resources/datapack/trimPattern.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { LiteralUnion } from 'sandstone/utils'
import type { JSONTextComponent } from '../jsonTextComponent'
import type { ITEMS } from '#arguments/generated'
import type { ITEMS } from '../../generated'
import type { JSONTextComponent } from '../../jsonTextComponent'
import type { LiteralUnion } from '#utils'

export type TrimPatternJSON = {
/** A resource location (In the resource pack) of the pattern that will be used as an overlay on the armor. */
Expand Down
8 changes: 4 additions & 4 deletions src/commands/implementations/server/datapack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ export class DataPackCommand extends CommandArguments {
/**
* Disable the specified pack.
*
* @param name Specifies the name of the data pack.
* @param name Specifies the name of the datapack.
*/
disable = (name: string) => this.finalCommand([name])

/**
* Enable the specified pack.
*
* @param name Specifies the name of the data pack.
* @param name Specifies the name of the datapack.
*
* @example
*
Expand All @@ -61,9 +61,9 @@ export class DataPackCommand extends CommandArguments {
enable = (name: string) => this.subCommand(['enable', name], DataPackEnableCommand, false)

/**
* List all data packs, or list only the available/enabled ones.
* List all datapacks, or list only the available/enabled ones.
*
* Hovering over the data packs in the chat output shows their description defined in their pack.mcmeta.
* Hovering over the datapacks in the chat output shows their description defined in their pack.mcmeta.
*
* @param typ `"available"` to only show available datapacks, `"enabled"` to only show enabled ones.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/commands/implementations/server/reload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ export class ReloadCommand extends CommandArguments {
protected NodeType = ReloadCommandNode

/**
* Reloads the current data packs.
* Reloads the current datapacks.
*
* If a data pack has invalid data (such as an invalid recipe format),
* If a datapack has invalid data (such as an invalid recipe format),
* changes are not applied and the game continues using the previous data.
*/
reload = () => this.finalCommand([])
Expand Down
28 changes: 18 additions & 10 deletions src/core/mcmeta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ const fetch = import('node-fetch')
export type MCMetaBranches = 'assets' | 'atlas' | 'data' | 'registries' | 'summary'

export class MCMetaCache {
readonly baseURL = 'https://raw.githubusercontent.com/misode/mcmeta/'
readonly base = '/misode/mcmeta/'

readonly path = path.join(process.env.WORKING_DIR as string, 'resources', 'cache', 'mcmeta')

readonly manifest = path.join(this.path, '..', 'mcmeta.json')
readonly manifest = path.join(this.path, '..', '..', 'mcmeta.json')

readonly lockFile = path.join(this.path, '..', 'lock-mcmeta.json')

Expand All @@ -32,7 +32,7 @@ export class MCMetaCache {
async load() {
this.loaded = true

if (await fs.pathExists(this.manifest)) {
if (!(await fs.pathExists(this.manifest))) {
return
}

Expand All @@ -42,7 +42,7 @@ export class MCMetaCache {

this.version = manifest.version

const getZip = async (branch: string) => new AdmZip(await (await (await fetch).default(`https://github.com/misode/mcmeta/archive/refs/heads/${branch}.zip`)).buffer())
const getZip = async (branch: string) => new AdmZip(await (await (await fetch).default(`https://github.com${this.base}archive/refs/heads/${branch}.zip`)).buffer())

if (!(await fs.pathExists(this.lockFile))) {
if (manifest.files.length > 10) {
Expand Down Expand Up @@ -172,10 +172,18 @@ export class MCMetaCache {
}

async save() {
await fs.writeFile(this.manifest, JSON.stringify({
branches: this.branches,
files: iterateEntries(this.files, (val) => val.text),
}))
if (this.files.size !== 0) {
await fs.writeFile(this.manifest, JSON.stringify({
branches: this.branches,
files: iterateEntries(this.files, (val) => val.text),
}))

await fs.writeFile(this.lockFile, JSON.stringify({
files: this.files,
version: this.version,
versionDate: this.versionDate,
}))
}
}

async get(branch: MCMetaBranches, relativePath: string): Promise<string>
Expand All @@ -200,7 +208,7 @@ export class MCMetaCache {
return existing
}

const req = await (await fetch).default(`${this.baseURL}/${branch}/${relativePath}`)
const req = await (await fetch).default(`https://raw.githubusercontent.com${this.base}${branch}/${relativePath}`)

const file = await (text ? req.text() : req.buffer())

Expand All @@ -210,7 +218,7 @@ export class MCMetaCache {
}

async getVersion(lockFile?: any) {
const fetchVersion = async () => JSON.parse(await (await (await fetch).default('https://api.github.com/repos/misode/mcmeta/commits?sha=summary')).text()).sha as string
const fetchVersion = async () => JSON.parse(await (await (await fetch).default(`https://api.github.com/repos${this.base}commits?sha=summary`)).text()).sha as string

const currentDate = Date()

Expand Down
2 changes: 1 addition & 1 deletion src/core/resources/custom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export abstract class CustomResourceClass extends ResourceClass<CustomResourceNo

super(
sandstoneCore,
{ packType: args.packType || sandstoneCore.pack.dataPack, extension: args.extension || 'json', encoding: args.encoding },
{ packType: args.packType || sandstoneCore.pack.dataPack(), extension: args.extension || 'json', encoding: args.encoding },
CustomResourceNode,
// eslint-disable-next-line no-nested-ternary
args.folder ? name.includes('/') ? name.split('/') : [name] : sandstoneCore.pack.resourceToPath(name, args.folder),
Expand Down
2 changes: 1 addition & 1 deletion src/core/resources/datapack/advancement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class AdvancementClass<CriteriaNames extends string = string> extends Res
public advancementJSON: NonNullable<AdvancementClassArguments['advancement']>

constructor(sandstoneCore: SandstoneCore, name: string, args: AdvancementClassArguments<CriteriaNames>) {
super(sandstoneCore, { packType: sandstoneCore.pack.dataPack, extension: 'json' }, AdvancementNode, sandstoneCore.pack.resourceToPath(name, ['advancements']), args)
super(sandstoneCore, { packType: sandstoneCore.pack.dataPack(), extension: 'json' }, AdvancementNode, sandstoneCore.pack.resourceToPath(name, ['advancements']), args)

this.advancementJSON = args.advancement as AdvancementJSON

Expand Down
2 changes: 1 addition & 1 deletion src/core/resources/datapack/damageType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class DamageTypeClass extends ResourceClass<DamageTypeNode> implements Co
public damageTypeJSON: NonNullable<DamageTypeClassArguments['damageType']>

constructor(sandstoneCore: SandstoneCore, name: string, args: DamageTypeClassArguments) {
super(sandstoneCore, { packType: sandstoneCore.pack.dataPack, extension: 'json' }, DamageTypeNode, sandstoneCore.pack.resourceToPath(name, ['trim_materials']), args)
super(sandstoneCore, { packType: sandstoneCore.pack.dataPack(), extension: 'json' }, DamageTypeNode, sandstoneCore.pack.resourceToPath(name, ['trim_materials']), args)

this.damageTypeJSON = args.damageType as DamageTypeJSON

Expand Down
2 changes: 1 addition & 1 deletion src/core/resources/datapack/itemModifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class ItemModifierClass extends ResourceClass<ItemModifierNode> implement
public itemModifierJSON: NonNullable<ItemModifierClassArguments['itemModifier']>

constructor(sandstoneCore: SandstoneCore, name: string, args: ItemModifierClassArguments) {
super(sandstoneCore, { packType: sandstoneCore.pack.dataPack, extension: 'json' }, ItemModifierNode, sandstoneCore.pack.resourceToPath(name, ['item_modifiers']), args)
super(sandstoneCore, { packType: sandstoneCore.pack.dataPack(), extension: 'json' }, ItemModifierNode, sandstoneCore.pack.resourceToPath(name, ['item_modifiers']), args)

this.itemModifierJSON = args.itemModifier as ItemModifierJSON

Expand Down
2 changes: 1 addition & 1 deletion src/core/resources/datapack/lootTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class LootTableClass extends ResourceClass<LootTableNode> {
public lootTableJSON: NonNullable<LootTableClassArguments['lootTable']>

constructor(sandstoneCore: SandstoneCore, name: string, args: LootTableClassArguments) {
super(sandstoneCore, { packType: sandstoneCore.pack.dataPack, extension: 'json' }, LootTableNode, sandstoneCore.pack.resourceToPath(name, ['loot_tables']), args)
super(sandstoneCore, { packType: sandstoneCore.pack.dataPack(), extension: 'json' }, LootTableNode, sandstoneCore.pack.resourceToPath(name, ['loot_tables']), args)

this.lootTableJSON = args.lootTable as LootTableJSON

Expand Down
6 changes: 3 additions & 3 deletions src/core/resources/datapack/mcfunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,12 @@ export type MCFunctionClassArguments = ({
*
* @example
*
* // Run every 5 ticks, including on data pack load.
* // Run every 5 ticks, including on datapack load.
* {
* runEvery: 5,
* }
*
* // Run every 5 ticks, but wait 5 ticks before data pack loads for 1st execution.
* // Run every 5 ticks, but wait 5 ticks before datapack loads for 1st execution.
* {
* runEvery: 5,
* runOnLoad: false,
Expand Down Expand Up @@ -199,7 +199,7 @@ export class _RawMCFunctionClass extends CallableResourceClass<MCFunctionNode> {
protected lazy: boolean

constructor(core: SandstoneCore, name: string, args: MCFunctionClassArguments) {
super(core, { packType: core.pack.dataPack, extension: 'mcfunction' }, MCFunctionNode, core.pack.resourceToPath(name, ['functions']), {
super(core, { packType: core.pack.dataPack(), extension: 'mcfunction' }, MCFunctionNode, core.pack.resourceToPath(name, ['functions']), {
...args,
addToSandstoneCore: args.lazy ? false : args.addToSandstoneCore,
})
Expand Down
2 changes: 1 addition & 1 deletion src/core/resources/datapack/predicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class PredicateClass extends ResourceClass<PredicateNode> implements List
public predicateJSON: NonNullable<PredicateClassArguments['predicate']>

constructor(sandstoneCore: SandstoneCore, name: string, args: PredicateClassArguments) {
super(sandstoneCore, { packType: sandstoneCore.pack.dataPack, extension: 'json' }, PredicateNode, sandstoneCore.pack.resourceToPath(name, ['predicates']), args)
super(sandstoneCore, { packType: sandstoneCore.pack.dataPack(), extension: 'json' }, PredicateNode, sandstoneCore.pack.resourceToPath(name, ['predicates']), args)

this.predicateJSON = args.predicate as PredicateJSON

Expand Down
Loading

0 comments on commit a8046a7

Please sign in to comment.