Skip to content
This repository was archived by the owner on Feb 17, 2022. It is now read-only.

Commit 2d3eb45

Browse files
authored
Merge pull request #4 from Lapis256/beta
add RawTextBuilder
2 parents 1ab0d15 + 02d2b2f commit 2d3eb45

File tree

10 files changed

+249
-49
lines changed

10 files changed

+249
-49
lines changed

src/command.js

Lines changed: 37 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,37 @@
1-
import { Commands, World } from "mojang-minecraft";
2-
import { mergeObject } from "./object.js";
3-
4-
5-
class CommandResult {
6-
constructor(isError, obj) {
7-
this.isError = isError;
8-
9-
mergeObject(this, obj);
10-
}
11-
}
12-
13-
export const Command = new (class {
14-
#run(command, dimension = "overworld") {
15-
if(typeof dimension === "string") {
16-
dimension = World.getDimension(dimension);
17-
}
18-
return Commands.run(command, dimension);
19-
}
20-
21-
run(command, dimension) {
22-
return this.#run(command, dimension);
23-
}
24-
25-
runSafe(command, dimension) {
26-
try {
27-
return new CommandResult(false, this.#run(command, dimension));
28-
}
29-
catch(e) {
30-
return new CommandResult(true, e);
31-
}
32-
}
33-
34-
selectorBuilder(selector, selectors) {
35-
if(!selectors) {
36-
if(selector.startsWith("@")) return selector;
37-
else return `"${selector}"`;
38-
}
39-
40-
const selectorArray = [];
41-
for(const key in selectors) {
42-
const rawValue = selectors[key];
43-
const value = rawValue.includes(" ") ? `"${rawValue}"` : rawValue;
44-
selectorArray.push(`${key}=${value}`);
45-
}
46-
return `${selector}[${selectorArray.join(",")}]`;
47-
}
48-
})();
1+
import { Dimension, Entity } from "mojang-minecraft";
2+
3+
4+
export default class Command {
5+
#run(command, object) {
6+
if(object.runCommand === undefined) return;
7+
return object.runCommand(command);
8+
}
9+
10+
static run(command, object) {
11+
return this.#run(command, object);
12+
}
13+
14+
static runSafe(command, object) {
15+
try {
16+
return new CommandResult(false, this.#run(command, dimension));
17+
}
18+
catch(e) {
19+
return new CommandResult(true, e);
20+
}
21+
}
22+
23+
static selectorBuilder(selector, selectors) {
24+
if(!selectors) {
25+
if(selector.startsWith("@")) return selector;
26+
else return `"${selector}"`;
27+
}
28+
29+
const selectorArray = [];
30+
for(const key in selectors) {
31+
const rawValue = selectors[key];
32+
const value = rawValue.includes(" ") ? `"${rawValue}"` : rawValue;
33+
selectorArray.push(`${key}=${value}`);
34+
}
35+
return `${selector}[${selectorArray.join(",")}]`;
36+
}
37+
};

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
export * from "./utils/index.js";
22
export { Event } from "./event.js";
33
export { GameRule } from "./gamerule.js";
4-
export { Command } from "./command.js";
4+
export { default as Command } from "./command.js";
55
export { World } from "./world.js";
66
export { Tick } from "./tick.js";
77
export { Tag } from "./tag.js";
88
export { Player } from "./player.js";
99
export { Scoreboard } from "./scoreboard.js";
1010
export { EventEmitter } from "./eventEmitter.js";
11+
export { RawTextBuilder } from "./rawTextBuilder/index.js";
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export default class RawTextComponent {
2+
name;
3+
#value;
4+
5+
constructor(value) {
6+
this.#value = value;
7+
}
8+
9+
#getValue() {
10+
if(this.#value.build === undefined) {
11+
return this.#value;
12+
}
13+
return this.#value.build();
14+
}
15+
16+
get value() {
17+
return this.#value;
18+
}
19+
20+
build() {
21+
return { [this.name]: this.#getValue() };
22+
}
23+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import Base from "./rawTextComponent.js";
2+
3+
4+
export default class ScoreComponent extends Base {
5+
name = "score";
6+
7+
#getName() {
8+
const { name } = this.value;
9+
if(name.build === undefined) {
10+
return name;
11+
}
12+
return name.build();
13+
}
14+
15+
#getScore() {
16+
const { objective, value } = this.value;
17+
const result = { name: this.#getName(), objective };
18+
19+
if(value === undefined) {
20+
return result;
21+
}
22+
return { ...result, value };
23+
}
24+
25+
build() {
26+
return { [this.name]: this.#getScore() };
27+
}
28+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import Base from "./rawTextComponent.js";
2+
import { SelectorBuilder, Selectors } from "../../selectorBuilder.js";
3+
4+
5+
export default class SelectorComponent extends Base{
6+
name = "selector";
7+
8+
constructor(selector) {
9+
if(selector instanceof SelectorBuilder ||
10+
Object.values(Selectors).includes(selector.toLowerCase())) {
11+
super(selector);
12+
return;
13+
}
14+
throw "type error";
15+
}
16+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import Base from "./rawTextComponent.js";
2+
3+
4+
export default class TextComponent extends Base {
5+
name = "text";
6+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import Base from "./rawTextComponent.js";
2+
3+
4+
export default class TranslateComponent extends Base {
5+
name = "translate";
6+
7+
#getWith() {
8+
return this.value.with.map(value => {
9+
if(value instanceof RawTextBuilder) {
10+
return value.build();
11+
}
12+
return value;
13+
});
14+
}
15+
16+
build() {
17+
const result = { [this.name]: this.value.translate };
18+
if(this.value.with <= 0) {
19+
return result;
20+
}
21+
return { ...result, with: this.#getWith() };
22+
}
23+
}

src/rawTextBuilder/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default as RawTextBuilder } from "./rawTextBuilder.js";
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import ScoreComponent from "./components/scoreComponent.js";
2+
import SelectorComponent from "./components/selectorComponent.js";
3+
import TextComponent from "./components/textComponent.js";
4+
import TranslateComponent from "./components/translateComponent.js";
5+
6+
7+
export default class RawTextBuilder {
8+
#values = [];
9+
10+
#add(value) {
11+
this.#values.push(value);
12+
return this;
13+
}
14+
15+
addScore(name, objective, value) {
16+
return this.#add(new ScoreComponent({ name, objective, value }));
17+
}
18+
19+
addSelector(value) {
20+
return this.#add(new SelectorComponent(value));
21+
}
22+
23+
addText(text) {
24+
return this.#add(new TextComponent(text));
25+
}
26+
27+
addTranslate(translate, ..._with) {
28+
return this.#add(new TranslateComponent({translate, with: _with}));
29+
}
30+
31+
#insert(index, value) {
32+
this.#values.splice(index, 0, value);
33+
return this;
34+
}
35+
36+
insertScore(index, name, objective, value) {
37+
return this.#insert(index, new ScoreComponent({ name, objective, value }));
38+
}
39+
40+
insertSelector(index, value) {
41+
return this.#insert(index, new SelectorComponent(value));
42+
}
43+
44+
insertText(index, text) {
45+
return this.#insert(index, new TextComponent(text));
46+
}
47+
48+
insertTranslate(index, translate, ..._with) {
49+
return this.#insert(index, new TranslateComponent({translate, with: _with}));
50+
}
51+
52+
remove(start, removeCount) {
53+
this.#values.splice(start, removeCount);
54+
return this;
55+
}
56+
57+
reverse() {
58+
this.#values = this.#values.reverse();
59+
return this;
60+
}
61+
62+
#buildValues() {
63+
return this.#values.map(v => v.build());
64+
}
65+
66+
build() {
67+
return { rawtext: this.#buildValues() };
68+
}
69+
70+
buildJSON() {
71+
return JSON.stringify(this.build());
72+
}
73+
}

src/selectorBuilder.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
export const Selectors = {
2+
allEntity: "@e",
3+
allPlayer: "@a",
4+
player: "@p",
5+
self: "@s"
6+
};
7+
Object.freeze(Selectors);
8+
9+
export class SelectorBuilder {
10+
#base;
11+
#selectors = [];
12+
#isPlayerName = false;
13+
14+
constructor(base) {
15+
const lower = base.toLowerCase();
16+
if(!Object.values(Selectors).includes(lower)) {
17+
this.#isPlayerName = true;
18+
this.#base = base;
19+
return
20+
}
21+
this.#base = lower;
22+
}
23+
24+
#buildSelectors() {
25+
if(!this.#selectors.length || this.#isPlayerName) {
26+
return "";
27+
}
28+
const selectors = this.#selectors.map(({ key, value }) => `${key}=${value}`);
29+
return `[${selectors.join(",")}]`;
30+
}
31+
32+
add(key, value) {
33+
this.#selectors.push({ key, value });
34+
return this;
35+
}
36+
37+
build() {
38+
return this.#base + this.#buildSelectors();
39+
}
40+
}

0 commit comments

Comments
 (0)