Skip to content
This repository has been archived by the owner on Aug 29, 2021. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
…server into parcel
  • Loading branch information
Levertion committed Sep 16, 2018
2 parents 75ac53b + 458182e commit 7e87de1
Show file tree
Hide file tree
Showing 19 changed files with 439 additions and 184 deletions.
13 changes: 0 additions & 13 deletions src/brigadier/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ import { MCFormat } from "../misc-functions";
* A blank command error
*/
export interface BlankCommandError {
/**
* Signifies that the error is a command error.
*/
_e: "1";
/**
* The code of this error, usable for translation?
*/
Expand Down Expand Up @@ -72,7 +68,6 @@ export class CommandErrorBuilder {

public createBlank(...substitutions: string[]): BlankCommandError {
return {
_e: "1",
code: this.code,
severity: this.severity,
substitutions,
Expand All @@ -81,14 +76,6 @@ export class CommandErrorBuilder {
}
}

/**
* Test if `T` is a command error
* @param T The thing to test
*/
export function isCommandError(T: any): T is CommandError {
return T._e === "1";
}

/**
* Transform `err` into a real command error.
* MODIFIES `err`
Expand Down
96 changes: 96 additions & 0 deletions src/data/lists/statics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,99 @@ export const particles = [
"minecraft:bubble_column_up",
"minecraft:nautilus"
];

export const entities = [
"area_effect_cloud",
"armor_stand",
"arrow",
"bat",
"blaze",
"boat",
"cave_spider",
"chicken",
"cod",
"cow",
"creeper",
"donkey",
"dolphin",
"dragon_fireball",
"drowned",
"elder_guardian",
"end_crystal",
"ender_dragon",
"enderman",
"endermite",
"evoker_fangs",
"evoker",
"experience_orb",
"eye_of_ender",
"falling_block",
"firework_rocket",
"ghast",
"giant",
"guardian",
"horse",
"husk",
"illusioner",
"item",
"item_frame",
"fireball",
"leash_knot",
"llama",
"llama_spit",
"magma_cube",
"minecart",
"chest_minecart",
"command_block_minecart",
"furnace_minecart",
"hopper_minecart",
"spawner_minecart",
"tnt_minecart",
"mule",
"mooshroom",
"ocelot",
"painting",
"parrot",
"pig",
"pufferfish",
"zombie_pigman",
"polar_bear",
"tnt",
"rabbit",
"salmon",
"sheep",
"shulker",
"shulker_bullet",
"silverfish",
"skeleton",
"skeleton_horse",
"slime",
"small_fireball",
"snow_golem",
"snowball",
"spectral_arrow",
"spider",
"squid",
"stray",
"tropical_fish",
"turtle",
"egg",
"ender_pearl",
"experience_bottle",
"potion",
"vex",
"villager",
"iron_golem",
"vindicator",
"witch",
"wither",
"wither_skeleton",
"wither_skull",
"wolf",
"zombie",
"zombie_horse",
"zombie_villager",
"phantom",
"lightning_bolt",
"trident"
];
37 changes: 27 additions & 10 deletions src/misc-functions/return-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,21 +162,38 @@ export class ReturnHelper<Errorkind extends BlankCommandError = CommandError> {
merge: ReturnedInfo<any, Errorkind>,
suggestOverride?: boolean
): this {
for (const val of [suggestOverride, this.suggesting]) {
if (typeof val === "boolean") {
if (val) {
this.mergeSuggestions(merge);
} else {
this.mergeSafe(merge);
}
return this;
let suggest: boolean | undefined;
if (suggestOverride !== undefined) {
suggest = suggestOverride;
} else {
if (this.suggesting !== undefined) {
suggest = this.suggesting;
}
}
this.mergeSuggestions(merge);
this.mergeSafe(merge);
switch (suggest) {
case true:
this.mergeSuggestions(merge);
break;
case false:
this.mergeSafe(merge);
break;
default:
this.mergeSuggestions(merge);
this.mergeSafe(merge);
}
return this;
}

public return<T, E>(
other: ReturnedInfo<T, Errorkind, E>
): ReturnedInfo<T, Errorkind, E> {
if (this.merge(other)) {
return this.succeed(other.data);
} else {
return this.failWithData(other.data);
}
}

public succeed<T extends undefined>(
data?: T
): ReturnSuccess<undefined, Errorkind>;
Expand Down
8 changes: 5 additions & 3 deletions src/parsers/get-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as coordParsers from "./minecraft/coordinates";
import * as itemParsers from "./minecraft/item";
import * as listParsers from "./minecraft/lists";
import { messageParser } from "./minecraft/message";
import * as namespaceParsers from "./minecraft/namespace-list";
import { functionParser, resourceParser } from "./minecraft/resources";

/**
Expand All @@ -24,15 +25,16 @@ const implementedParsers: { [id: string]: Parser } = {
"minecraft:block_state": blockParsers.stateParser,
"minecraft:color": listParsers.colorParser,
"minecraft:entity_anchor": listParsers.entityAnchorParser,
"minecraft:entity_summon": namespaceParsers.summonParser,
"minecraft:function": functionParser,
"minecraft:item_enchantment": listParsers.enchantmentParser,
"minecraft:item_enchantment": namespaceParsers.enchantmentParser,
"minecraft:item_predicate": itemParsers.predicate,
"minecraft:item_slot": listParsers.itemSlotParser,
"minecraft:item_stack": itemParsers.stack,
"minecraft:message": messageParser,
"minecraft:mob_effect": listParsers.mobEffectParser,
"minecraft:mob_effect": namespaceParsers.mobEffectParser,
"minecraft:operation": listParsers.operationParser,
"minecraft:particle": listParsers.particleParser,
"minecraft:particle": namespaceParsers.particleParser,
"minecraft:resource_location": resourceParser,
"minecraft:rotation": coordParsers.rotation,
"minecraft:scoreboard_slot": listParsers.scoreBoardSlotParser,
Expand Down
26 changes: 1 addition & 25 deletions src/parsers/minecraft/lists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@ import { StringReader } from "../../brigadier/string-reader";
import { COLORS } from "../../colors";
import { itemSlots } from "../../data/lists/item-slot";
import { scoreboardSlots } from "../../data/lists/scoreboard-slot";
import {
anchors,
effects,
enchantments,
operations,
particles
} from "../../data/lists/statics";
import { anchors, operations } from "../../data/lists/statics";
import { ReturnHelper } from "../../misc-functions";
import { Parser, ParserInfo, ReturnedInfo } from "../../types";

Expand Down Expand Up @@ -61,33 +55,15 @@ const entityAnchorError = new CommandErrorBuilder(
);
export const entityAnchorParser = new ListParser(anchors, entityAnchorError);

const enchantmentError = new CommandErrorBuilder(
"enchantment.unknown",
"Unknown enchantment: %s"
);
export const enchantmentParser = new ListParser(enchantments, enchantmentError);

const slotError = new CommandErrorBuilder("slot.unknown", "Unknown slot '%s'");
export const itemSlotParser = new ListParser(itemSlots, slotError);

const mobEffectError = new CommandErrorBuilder(
"effect.effectNotFound",
"Unknown effect: %s"
);
export const mobEffectParser = new ListParser(effects, mobEffectError);

const operationError = new CommandErrorBuilder(
"arguments.operation.invalid",
"Invalid operation"
);
export const operationParser = new ListParser(operations, operationError);

const particleError = new CommandErrorBuilder(
"particle.notFound",
"Unknown particle: %s"
);
export const particleParser = new ListParser(particles, particleError);

const scoreboardSlotError = new CommandErrorBuilder(
"argument.scoreboardDisplaySlot.invalid",
"Unknown display slot '%s'"
Expand Down
79 changes: 79 additions & 0 deletions src/parsers/minecraft/namespace-list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { CommandErrorBuilder } from "../../brigadier/errors";
import { StringReader } from "../../brigadier/string-reader";
import {
effects,
enchantments,
entities,
particles
} from "../../data/lists/statics";
import {
convertToNamespace,
parseNamespaceOption,
ReturnHelper,
stringifyNamespace
} from "../../misc-functions";
import { ContextChange, Parser, ParserInfo, ReturnedInfo } from "../../types";

export class NamespaceListParser implements Parser {
private readonly error: CommandErrorBuilder;
private readonly options: string[];
public constructor(options: string[], errorBuilder: CommandErrorBuilder) {
this.options = options;
this.error = errorBuilder;
}
public parse(
reader: StringReader,
info: ParserInfo
): ReturnedInfo<ContextChange> {
const helper = new ReturnHelper(info);
const start = reader.cursor;
const result = parseNamespaceOption(
reader,
this.options.map((v, _) => convertToNamespace(v))
);
if (helper.merge(result)) {
return helper.succeed();
} else {
if (result.data) {
return helper
.addErrors(
this.error.create(
start,
reader.cursor,
stringifyNamespace(result.data)
)
)
.succeed();
} else {
return helper.fail();
}
}
}
}

const summonError = new CommandErrorBuilder(
"entity.notFound",
"Unknown entity: %s"
);
export const summonParser = new NamespaceListParser(entities, summonError);

const enchantmentError = new CommandErrorBuilder(
"enchantment.unknown",
"Unknown enchantment: %s"
);
export const enchantmentParser = new NamespaceListParser(
enchantments,
enchantmentError
);

const mobEffectError = new CommandErrorBuilder(
"effect.effectNotFound",
"Unknown effect: %s"
);
export const mobEffectParser = new NamespaceListParser(effects, mobEffectError);

const particleError = new CommandErrorBuilder(
"particle.notFound",
"Unknown particle: %s"
);
export const particleParser = new NamespaceListParser(particles, particleError);

0 comments on commit 7e87de1

Please sign in to comment.