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

Commit 811adba

Browse files
authored
Add files via upload
1 parent db8357c commit 811adba

File tree

10 files changed

+310
-0
lines changed

10 files changed

+310
-0
lines changed

src/axisAlignedBB.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export class AxisAlignedBB {
2+
// TODO
3+
}

src/entity.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export class Entity {
2+
// TODO
3+
}

src/errors.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Error {
2+
name = "Error";
3+
constructor(message) {
4+
this.message = message;
5+
}
6+
toString() {
7+
return this.message;
8+
}
9+
}
10+
11+
export class EventNotDefined extends Error {
12+
name = "EventNotDefined";
13+
constructor(eventName) {
14+
super(`${eventName} event is not defined.`);
15+
}
16+
}
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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { World } from "Minecraft";
2+
import { EventNotDefined } from "./errors.js";
3+
import { print } from "./utils.js";
4+
5+
6+
export const Event = new (class {
7+
on(eventName, callback) {
8+
const event = World.events[eventName];
9+
if(!event) {
10+
throw new EventNotDefined(eventName);
11+
}
12+
const newCallback = eventData => {
13+
try {
14+
callback(eventData);
15+
}
16+
catch(e) {
17+
print("Event Running Error: " + e);
18+
}
19+
}
20+
event.subscribe(newCallback);
21+
return newCallback;
22+
}
23+
24+
off(eventName, callback) {
25+
const event = World.events[eventName];
26+
if(!event) {
27+
throw new EventNotDefined(eventName);
28+
}
29+
event.unsubscribe(callback);
30+
}
31+
})();

src/gamerule.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { Commands } from "Minecraft";
2+
import { Command } from "./command.js";
3+
import { print } from "./utils.js";
4+
5+
6+
export const GameRule = new (class {
7+
#gameruleValueConverter(value) {
8+
switch(value) {
9+
case "true":
10+
return true;
11+
case "false":
12+
return false;
13+
default:
14+
return Number(rawValue);
15+
}
16+
}
17+
18+
#gameruleParser(text) {
19+
const [ key, rawValue ] = text.split(" = ");
20+
return [ key, this.#gameruleValueConverter(rawValue) ];
21+
}
22+
23+
get(ruleName) {
24+
try {
25+
const { statusMessage } = Command.run("gamerule " + ruleName);
26+
const [ _, value ] = this.#gameruleParser(statusMessage);
27+
return value;
28+
}
29+
catch {
30+
return;
31+
}
32+
}
33+
34+
set(ruleName, value) {
35+
try {
36+
Command.run(`gamerule ${ruleName} ${value}`);
37+
}
38+
catch {
39+
return false;
40+
}
41+
return true;
42+
}
43+
44+
all() {
45+
const { statusMessage } = Command.run("gamerule");
46+
const gamerules = statusMessage.split(", ").map(this.#gameruleParser);
47+
return Object.fromEntries(gamerules);
48+
}
49+
})();

src/player.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { World } from "Minecraft";
2+
import { Command } from "./command.js";
3+
import { Tag } from "./tag.js";
4+
5+
6+
export class Player {
7+
#player;
8+
#tagSelector;
9+
10+
constructor(player) {
11+
this.#player = player;
12+
this.#tagSelector = Command.selectorBuilder(player.name);
13+
14+
for(const attr in player) {
15+
this[attr] = player[attr];
16+
}
17+
}
18+
19+
static getAll() {
20+
return World.getPlayers().map(Player.get);
21+
}
22+
23+
getAllTags() {
24+
return Tag.getAllTags(this.#tagSelector);
25+
}
26+
27+
hasTag(tag) {
28+
return Tag.hasTag(this.#tagSelector, tag);
29+
}
30+
31+
addTag(tag) {
32+
return Tag.addTag(this.#tagSelector, tag);
33+
}
34+
35+
removeAllTags() {
36+
return Tag.removeAllTags(this.#tagSelector);
37+
}
38+
39+
removeTag(tag) {
40+
return Tag.removeTag(this.#tagSelector, tag);
41+
}
42+
}

src/scoreboard.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { Command } from "./command.js";
2+
3+
4+
export const Scoreboard = new (class {
5+
#ListRegex = /-\s(?<name>.*?):\s/g;
6+
7+
addObjective(name, displayName) {
8+
const _displayName = displayName ? displayName : name;
9+
Command.runSafe(`scoreboard objectives add "${name}" dummy "${_displayName}"`);
10+
}
11+
12+
getAllObjectives() {
13+
try {
14+
const { statusMessage } = Command.run(`scoreboard objectives list`);
15+
return [...statusMessage.matchAll(this.#ListRegex)].map(m => m.groups.name);
16+
}
17+
catch {
18+
return [];
19+
}
20+
}
21+
22+
isExistObjective(name) {
23+
return getAllObjectives().includes(name);
24+
}
25+
})();

src/tag.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { Player, Entity } from "Minecraft";
2+
import { Command } from "./command.js";
3+
4+
5+
export const Tag = new (class {
6+
#TagRegex = /§a(?<tag>.*?)§r(?:,\s|$)/g;
7+
8+
getAllTags(selector) {
9+
const { statusMessage } = Command.run(`tag ${selector} list`);
10+
return [...statusMessage.matchAll(this.#TagRegex)].map(m => m.groups.tag);
11+
}
12+
13+
hasTag(selector, tag) {
14+
return this.getAllTags(selector).includes(tag);
15+
}
16+
17+
addTag(selector, tag) {
18+
if(this.hasTag(selector, tag)) return false;
19+
try {
20+
Command.run(`tag ${selector} add "${tag}"`);
21+
return true;
22+
}
23+
catch {
24+
return false;
25+
}
26+
}
27+
28+
removeAllTags(selector) {
29+
this.getAllTags(selector).forEach(t => this.removeTag(selector, t));
30+
return true;
31+
}
32+
33+
removeTag(selector, tag) {
34+
if(!this.hasTag(selector, tag)) return false;
35+
try {
36+
Command.run(`tag ${selector} remove "${tag}"`);
37+
return true;
38+
}
39+
catch {
40+
return false;
41+
}
42+
}
43+
})();

src/tick.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { Event } from "./event.js";
2+
import { print } from "./utils.js";
3+
4+
5+
class Interval {
6+
totalTick = 1;
7+
executed = false;
8+
constructor(callback, interval, once) {
9+
this.callback = callback;
10+
this.interval = interval;
11+
this.once = once;
12+
}
13+
isRemovable() {
14+
return this.once && this.executed;
15+
}
16+
tick() {
17+
if(this.totalTick++ % this.interval === 0) {
18+
this.callback();
19+
this.executed = true;
20+
}
21+
}
22+
}
23+
24+
class IntervalHandler {
25+
intervals = {};
26+
intervalID = 0;
27+
tick() {
28+
for(const id in this.intervals) {
29+
const interval = this.intervals[id];
30+
interval.tick();
31+
if(interval.isRemovable()) {
32+
this.remove(id);
33+
}
34+
}
35+
}
36+
add(interval) {
37+
const id = this.intervalID++;
38+
this.intervals[id] = interval;
39+
return id;
40+
}
41+
remove(id) {
42+
delete this.intervals[id];
43+
}
44+
}
45+
46+
export const Tick = new (class {
47+
#intervalHandler = new IntervalHandler();
48+
49+
constructor() {
50+
Event.on("tick", _ => this.#intervalHandler.tick());
51+
}
52+
53+
setInterval(callback, interval) {
54+
return intervalHandler.add(
55+
new Interval(callback, interval, false)
56+
);
57+
}
58+
59+
clearInterval(id) {
60+
intervalHandler.remove(id);
61+
}
62+
63+
setTimeout(callback, timeout) {
64+
return intervalHandler.add(
65+
new Interval(callback, timeout, true)
66+
);
67+
}
68+
69+
clearTimeout(id) {
70+
intervalHandler.remove(id);
71+
}
72+
})();

src/utils.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Command } from "./command.js";
2+
3+
4+
export function print(text) {
5+
// TODO
6+
7+
//Commands.run(`tellraw @a {"rawtext":[{"text":"${text}"}]}`);
8+
// Commands.run(`say {"rawtext":[{"text":"${text}"}]}`);
9+
//Commands.run(`tellraw @a {"rawtext":[{"text":"${escape(text)}"}]}`);
10+
// const rawText = JSON.stringify({rawtext: [{text: text}]});
11+
// commandRun("tellraw @a " + rawText);
12+
Command.run(`say Debug: ${text}`);
13+
}
14+
15+
export function pjson(json) {
16+
print(JSON.stringify(json));
17+
}

0 commit comments

Comments
 (0)