Skip to content

Commit

Permalink
Merge pull request #35 from TheMrZZ/develop
Browse files Browse the repository at this point in the history
Improvements to savePack
  • Loading branch information
TheMrZZ authored Nov 21, 2020
2 parents 3cd49b5 + 6f5d53f commit b6d76f2
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 30 deletions.
1 change: 1 addition & 0 deletions src/_internals/arguments/resources/AdvancementTriggers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type { ObjectOrArray } from './predicate'

// The advancement triggers
type Trigger<NAME extends string, CONDITIONS extends Record<string, unknown>> = {
/** The trigger for this advancement; specifies what the game should check for the advancement. */
trigger: NAME

/** All the conditions that need to be met when the trigger gets activated. */
Expand Down
43 changes: 24 additions & 19 deletions src/_internals/arguments/resources/advancement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,27 @@ export type AdvancementType<CRITERIA_NAMES extends string> = {
* Has no effect on root advancements themselves, but still affects all their children.
* Defaults to `false`. */
hidden?: boolean

/** The optional directory for the background to use in this advancement tab (used only for the root advancement). */
background?: string
}

/** The required criteria that have to be met. */
/**
* The required criteria that have to be met.
*
* Must be an object: the keys are the criteria names, the values are objects with a `trigger` and `conditions`.
*
* @example
*
* {
* criteria: {
* bred_two_cows: {
* trigger: 'minecraft:bred_animals',
* conditions: { ... },
* }
* }
* }
*/
criteria: Record<CRITERIA_NAMES, AdvancementTriggers>

/** An optional list of requirements (all the <criteriaNames>).
Expand All @@ -65,22 +83,9 @@ export type AdvancementType<CRITERIA_NAMES extends string> = {
/** A function to run. Function tags are not allowed. */
function?: string | McFunctionReturn<[]>
}
} & (
// Root advancement can specify a background - non-root can't.
{
display?: {
/** The optional directory for the background to use in this advancement tab (used only for the root advancement). */
background?: string
}

/** The optional parent advancement directory of this advancement.
* If this field is absent, this advancement is a root advancement.
* Circular references cause a loading failure. */
parent?: undefined
} | {
/** The optional parent advancement directory of this advancement.
* If this field is absent, this advancement is a root advancement.
* Circular references cause a loading failure. */
parent: string
}
)
/** The optional parent advancement directory of this advancement.
* If this field is absent, this advancement is a root advancement.
* Circular references cause a loading failure. */
parent?: string
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable camelcase */
import { LiteralUnion } from '@/generalTypes'
import type { LiteralUnion } from '@/generalTypes'
import type { ENTITY_TYPES, MOB_EFFECTS } from '@arguments/generated'
import type { Tag } from '@resources'
import type { DistanceCriterion, EffectCriterion } from './basic_criteria'
Expand Down Expand Up @@ -48,7 +48,7 @@ export type EntityCriterion = Partial<{
team: string

/** An entity ID. */
type: ENTITY_TYPES | Tag<'entity_types'>
type: LiteralUnion<ENTITY_TYPES> | Tag<'entity_types'>

/** The entity which this entity is targeting for attacks. */
targeted_entity: EntityCriterion
Expand Down
2 changes: 1 addition & 1 deletion src/_internals/datapack/Datapack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ export default class Datapack {
* @param name The name of the Datapack
* @param options The save options
*/
save = (name: string, options: SaveOptions = {}): Promise<void> => {
save = (name: string, options: SaveOptions = {}) => {
if (!options.dryRun) {
console.log(chalk`⌛ {gray Starting compilation...}`)
}
Expand Down
14 changes: 11 additions & 3 deletions src/_internals/datapack/saveDatapack.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { JsonTextComponent } from '@arguments'
import chalk from 'chalk'
import fs from 'graceful-fs'
import path from 'path'
Expand Down Expand Up @@ -51,8 +52,10 @@ export type SaveOptions = {
/**
* The description of the datapack.
* Corresponds to the `pack.description` property of the `pack.mcmeta` file.
*
* Can be a string or a JSON Text Component.
*/
description?: string
description?: JsonTextComponent

/**
* The format version of the datapack.
Expand Down Expand Up @@ -160,7 +163,7 @@ function saveResource<T extends ResourceTypes>(
* @param name The name of the Datapack
* @param options The save options.
*/
export async function saveDatapack(resources: ResourcesTree, name: string, options: SaveOptions): Promise<void> {
export async function saveDatapack(resources: ResourcesTree, name: string, options: SaveOptions) {
// This ensure the function is async, and can be await
const writeFileToDisk = async <U extends ExtendedResourceTypes>(info: SaveFileObject) => {
const func = options?.customFileHandler ?? writeFile
Expand All @@ -187,7 +190,7 @@ export async function saveDatapack(resources: ResourcesTree, name: string, optio
rootPath = path.join(rootPath, name)

if (options.description !== undefined) {
packMcMeta.pack.description = options.description
packMcMeta.pack.description = options.description as string
}

if (options.formatVersion !== undefined) {
Expand Down Expand Up @@ -284,8 +287,13 @@ export async function saveDatapack(resources: ResourcesTree, name: string, optio
if (!options.dryRun && !options.customFileHandler) {
console.log(chalk`{greenBright ✓ Successfully wrote datapack to "${rootPath}".} {gray (${promises.length.toLocaleString()} files - ${(Date.now() - start).toLocaleString()}ms)}`)
}

return {
destination: rootPath,
}
} catch (e) {
console.error(e)
console.log(chalk`{redBright ✗ Failed to write datapack. See above for additional information.}`)
throw e
}
}
9 changes: 4 additions & 5 deletions tests/test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/** eslint-disable */
import {
effect, give, summon, tellraw,
} from '../src/commands'
Expand All @@ -12,16 +13,14 @@ import { Menu } from './menu'

datapack.resources = new ResourcesTree()

/** An advancement granted to a survival player who bred 2 cows. */
// Advancement granted to survival players breeding 2 cows
Advancement('bred_cows', {
criteria: {
player_bred_cows: {
bred_two_cows: {
trigger: 'minecraft:bred_animals',
conditions: {
child: { type: 'minecraft:cow' },
player: {
gamemode: 'survival',
},
player: { gamemode: 'survival' },
},
},
},
Expand Down

0 comments on commit b6d76f2

Please sign in to comment.