Skip to content

Commit

Permalink
Added possibility to set Scores to a given Data Point
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMrZZ committed Apr 18, 2021
1 parent 0a091fc commit d6df2c7
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 35 deletions.
80 changes: 51 additions & 29 deletions src/datapack/Datapack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import chalk from 'chalk'
import { CommandsRoot } from '@commands'
import { Flow } from '@flow'
import { ObjectiveClass, SelectorCreator } from '@variables'
import { DataInstance, TargetlessDataInstance } from '@variables/Data'
import { DataInstance, DataPointInstance, TargetlessDataInstance } from '@variables/Data'

import { BasePathClass } from './BasePath'
import { toMCFunctionName } from './minecraft'
Expand Down Expand Up @@ -506,34 +506,56 @@ export default class Datapack {
return this.getCreateObjective('sandstone', 'dummy', [{ text: 'Sandstone', color: 'gold' }, ' internals'])
}

/**
* Creates a dynamic numeric variable, represented by an anonymous & unique score.
*
* @param initialValue The initial value of the variable. If left unspecified,
* or if `undefined`, then the score will not be initialized.
*
* @param name A name that can be useful for debugging.
*/
Variable = (initialValue?: number | Score | undefined, name?: string) => {
// Get the objective
const datapack = this.commandsRoot.Datapack
const score = datapack.rootObjective

// Get the specific anonymous score
const id = Datapack.anonymousScoreId
Datapack.anonymousScoreId += 1
const anonymousScore = score(`${name ?? 'anon'}_${datapack.packUid}_${id}`)

if (initialValue !== undefined) {
if (this.currentFunction !== null) {
anonymousScore.set(initialValue)
} else {
this.initCommands.push(['scoreboard', 'players', 'set', anonymousScore.target, anonymousScore.objective, initialValue])
}
}

return anonymousScore
}
Variable: (
(
/**
* Creates a dynamic numeric variable, represented by an anonymous & unique score.
*
* @param initialValue The initial value of the variable. If left unspecified,
* or if `undefined`, then the score will not be initialized.
*
* @param name A name that can be useful for debugging.
*/
(initialValue?: number | Score | undefined, name?: string) => Score
) & (
/**
* Creates a dynamic numeric variable, represented by an anonymous & unique score.
*
* @param nbt The NBT value to set the Variable to.
*
* @param scale The scale to multiply the value by. Defaults to 1.
*
* @param name A name that can be useful for debugging.
*/
(nbt: DataPointInstance, scale?: number, name?: string) => Score
)
) = (initialValue?: number | Score | undefined | DataPointInstance, nameOrScale?: string | number, maybeName?: string) => {
// Get the objective
const datapack = this.commandsRoot.Datapack
const score = datapack.rootObjective

if (initialValue instanceof DataPointInstance) {
// If the value is a data point, leverage the .set method
return this.Variable(undefined, maybeName).set(initialValue, nameOrScale as number)
}

const name = nameOrScale as string | undefined

// Get the specific anonymous score
const id = Datapack.anonymousScoreId
Datapack.anonymousScoreId += 1
const anonymousScore = score(`${name ?? 'anon'}_${datapack.packUid}_${id}`)

if (initialValue !== undefined) {
if (this.currentFunction !== null) {
anonymousScore.set(initialValue)
} else {
this.initCommands.push(['scoreboard', 'players', 'set', anonymousScore.target, anonymousScore.objective, initialValue])
}
}

return anonymousScore
}

Selector = SelectorCreator.bind(this)

Expand Down
29 changes: 25 additions & 4 deletions src/variables/Score.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import type {
} from 'src/arguments'
import type { CommandsRoot } from '@commands'
import type { Datapack } from '@datapack'
import type { ConditionClass } from '@variables/abstractClasses'
import type { Range } from '..'
import type { ConditionClass, Range } from '@variables'
import type { DataPointInstance } from './Data'
import type { ObjectiveClass } from './Objective'

type PlayersTarget = number | MultipleEntitiesArgument
Expand Down Expand Up @@ -127,8 +127,29 @@ export class Score<OBJ_CRITERION extends string | undefined = string | undefined
*/
set(amountOrTargetScore: number | Score): this

set(...args: OperationArguments) {
return this.unaryOperation('set', '=', ...args)
/**
* Set the current entity's score to the given NBT value, with the given scale.
*
* @param nbt The Data Point to set the score to.
*
* @param scale The scale factor. Defaults to 1.
*/
set(nbt: DataPointInstance, scale?: number): this

set(...args: OperationArguments | [DataPointInstance, number?]) {
if (args.length === 2) {
// eslint-disable-next-line prefer-const
let [data, scale] = args as [DataPointInstance, number?]

if (typeof scale === 'number') {
scale = scale ?? 1
this.commandsRoot.execute.store.result.score(this).run.data.get[data.type](data.currentTarget as any, data.path, scale)

return this
}
}

return this.unaryOperation('set', '=', ...args as OperationArguments)
}

/**
Expand Down
9 changes: 7 additions & 2 deletions src/variables/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,20 @@ export function rotationParser<T extends unknown>(rotation: T): (
return isRawRotation(rotation) ? new VectorClass(rotation) : rotation as any
}

export const nbtParser = (nbt: NBTObject) => util.inspect(nbt, {
function removeSpaces(s: string) {
let inside = 0
// eslint-disable-next-line
return s.replace(/ +|['"]/g, (m) => (m === '"\'' ? (inside ^= 1, '"\'') : inside ? m : ''))
}
export const nbtParser = (nbt: NBTObject) => removeSpaces(util.inspect(nbt, {
depth: null,
showHidden: false,
compact: true,
maxArrayLength: null,
maxStringLength: null,
breakLength: Infinity,
colors: false,
})
}))

// Sanitize score values. null => '', Infinity => '', any number => itself
export const sanitizeValue = (value: number | null): string => {
Expand Down

0 comments on commit d6df2c7

Please sign in to comment.