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

Commit ffb03db

Browse files
committed
support beta
1 parent 7438f3a commit ffb03db

File tree

10 files changed

+61
-29
lines changed

10 files changed

+61
-29
lines changed

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export * from "./src/utils.js";
22
export { Event } from "./src/event.js";
33
export { GameRule } from "./src/gamerule.js";
44
export { Command } from "./src/command.js";
5+
export { World } from "./src/world.js";
56
export { Tick } from "./src/tick.js";
67
export { Tag } from "./src/tag.js";
78
export { Player } from "./src/player.js";

src/command.js

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,24 @@
1-
import { Commands } from "Minecraft";
2-
import { CommandExecutionError } from "./errors.js";
1+
import { Commands, World } from "mojang-minecraft";
32

43

54
export const Command = new (class {
6-
#run(command, dimension) {
7-
if(dimension) {
8-
return Commands.run(command, dimension);
5+
#run(command, dimension = "overworld") {
6+
if(typeof dimension === "string") {
7+
dimension = World.getDimension(dimension);
98
}
10-
return Commands.run(command);
9+
return Commands.run(command, dimension);
1110
}
1211

1312
run(command, dimension) {
14-
try {
15-
return this.#run(command, dimension);
16-
}
17-
catch(e) {
18-
const status = JSON.parse(e);
19-
throw new CommandExecutionError(command, status);
20-
}
13+
return this.#run(command, dimension);
2114
}
2215

2316
runSafe(command, dimension) {
2417
try {
2518
return this.#run(command, dimension);
2619
}
2720
catch(e) {
28-
return JSON.parse(e);
21+
return e;
2922
}
3023
}
3124

src/dimension.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Command } from "./command.js";
2+
3+
4+
export class Dimension {
5+
#dimension;
6+
7+
constructor(dimension) {
8+
this.#dimension = dimension;
9+
10+
for(const attr in dimension) {
11+
this[attr] = dimension[attr];
12+
}
13+
}
14+
commandRun(command) {
15+
return Command.run(command, this.#dimension);
16+
}
17+
commandRunSafe(command) {
18+
return Command.runSafe(command, this.#dimension);
19+
}
20+
}

src/errors.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,3 @@ export class EventNotDefined extends Error {
1414
super(`${eventName} event is not defined.`);
1515
}
1616
}
17-
18-
export class CommandExecutionError extends Error {
19-
name = "CommandExecutionError";
20-
constructor(command, { statusMessage, statusCode }) {
21-
super(statusMessage);
22-
this.code = statusCode;
23-
this.command = command;
24-
}
25-
}

src/event.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { World } from "Minecraft";
1+
import { World } from "mojang-minecraft";
22
import { EventNotDefined } from "./errors.js";
33
import { print } from "./utils.js";
44

src/gamerule.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import { Commands } from "Minecraft";
21
import { Command } from "./command.js";
3-
import { print } from "./utils.js";
42

53

64
export const GameRule = new (class {

src/player.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { World } from "Minecraft";
1+
import { World } from "mojang-minecraft";
22
import { Command } from "./command.js";
33
import { Tag } from "./tag.js";
44

@@ -39,4 +39,8 @@ export class Player {
3939
removeTag(tag) {
4040
return Tag.removeTag(this.#tagSelector, tag);
4141
}
42+
43+
toString() {
44+
return this.name;
45+
}
4246
}

src/tag.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Player, Entity } from "Minecraft";
1+
import { Player, Entity } from "mojang-minecraft";
22
import { Command } from "./command.js";
33

44

src/utils.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Command } from "./command.js";
33

44
export function print(...obj) {
55
const rawtext = JSON.stringify({
6-
rawtext: [{ text: obj.join(" ") }]
6+
rawtext: [{ text: obj.map(String).join(" ") }]
77
}).replaceAll("\\r\\n", "\\n");
88
Command.run("tellraw @a " + rawtext);
99
}
@@ -48,6 +48,9 @@ export function toJson(data, indent = 4) {
4848
}
4949
return obj;
5050

51+
case "undefined":
52+
return null;
53+
5154
default:
5255
return value;
5356
}

src/world.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { World as _World } from "mojang-minecraft";
2+
import { Dimension } from "./dimension.js";
3+
import { Player } from "./player.js";
4+
5+
6+
export const World = new (class {
7+
#excludeAttrs = ["getDimension", "getPlayers"];
8+
9+
constructor() {
10+
for(const attr in _World) {
11+
if(this.#excludeAttrs.includes(attr)) continue;
12+
this[attr] = _World[attr];
13+
}
14+
}
15+
getDimension(name) {
16+
const dimension = _World.getDimension(name);
17+
return new Dimension(dimension);
18+
}
19+
getPlayers() {
20+
return Player.getAll();
21+
}
22+
})();

0 commit comments

Comments
 (0)