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

Commit a4e73fe

Browse files
authored
Merge pull request #6 from Lapis256/beta/1.18.20
add gamemode.js
2 parents 1675093 + 2b65de8 commit a4e73fe

File tree

6 files changed

+80
-98
lines changed

6 files changed

+80
-98
lines changed

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import "./src/commandInitializer.js";
1313
export * from "./src/utils/index.js";
1414
export * from "./src/debug/index.js";
1515
export { Tick } from "./src/tick.js";
16+
// export { Player } from "./src/player.js";
1617
export { Command } from "./src/command.js";
1718
export { Dimension } from "./src/dimension.js";
19+
export { Gamemode } from "./src/gamemode.js";
1820
export { RawTextBuilder } from "./src/rawTextBuilder/index.js";

src/gamemode.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { GameMode } from "mojang-minecraft";
2+
3+
4+
class _GameMode {
5+
#checkGameMode(player, mode) {
6+
try {
7+
player.runCommand(`testfor @s[m=${mode}]`);
8+
return true;
9+
} catch {
10+
return false;
11+
}
12+
}
13+
14+
isCreative(player) {
15+
return this.#checkGameMode(player, "c");
16+
}
17+
18+
isSurvival(player) {
19+
return this.#checkGameMode(player, "s");
20+
}
21+
22+
isAdventure(player) {
23+
return this.#checkGameMode(player, "a");
24+
}
25+
26+
isDefault(player) {
27+
return this.#checkGameMode(player, "d");
28+
}
29+
30+
get(player) {
31+
if(this.isCreative(player)) {
32+
return GameMode.creative;
33+
}
34+
if(this.isSurvival(player)) {
35+
return GameMode.survival;
36+
}
37+
if(this.isAdventure(player)) {
38+
return GameMode.adventure;
39+
}
40+
}
41+
}
42+
43+
export const Gamemode = new _GameMode();

src/player.js

Lines changed: 24 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,24 @@
1-
import { World } from "mojang-minecraft";
2-
import { Command } from "./command.js";
3-
import { Tag } from "./tag.js";
4-
import { mergeObject } from "./object.js";
5-
import { range } from "./utils/index.js";
6-
7-
8-
export class Player {
9-
#player;
10-
#tagSelector;
11-
#inventory;
12-
13-
constructor(player) {
14-
mergeObject(this, player);
15-
16-
this.#player = player;
17-
this.player = player;
18-
this.#tagSelector = Command.selectorBuilder(player.name);
19-
20-
this.#inventory = player.getComponent("inventory").container;
21-
}
22-
23-
static getAll() {
24-
return World.getPlayers().map(p => new Player(p));
25-
}
26-
27-
get items() {
28-
return range(this.#inventory.size)
29-
.map(i => this.#inventory.getItem(i));
30-
}
31-
32-
getAllTags() {
33-
return Tag.getAllTags(this.#tagSelector);
34-
}
35-
36-
hasTag(tag) {
37-
return Tag.hasTag(this.#tagSelector, tag);
38-
}
39-
40-
addTag(tag) {
41-
return Tag.addTag(this.#tagSelector, tag);
42-
}
43-
44-
removeAllTags() {
45-
return Tag.removeAllTags(this.#tagSelector);
46-
}
47-
48-
removeTag(tag) {
49-
return Tag.removeTag(this.#tagSelector, tag);
50-
}
51-
52-
toString() {
53-
return this.name;
54-
}
55-
}
1+
import { RawTextBuilder } from "./rawTextBuilder/index.js";
2+
3+
4+
export class Player {
5+
#player;
6+
7+
constructor(player) {
8+
this.#player = player;
9+
}
10+
11+
sendRawtext(text, ..._with) {
12+
const rawText = new RawTextBuilder()
13+
.addTranslate(text, ..._with.map(String))
14+
.buildJson();
15+
this.#player.runCommand("tellraw @s " + rawText);
16+
}
17+
18+
showRawtext(text, ..._with) {
19+
const rawText = new RawTextBuilder()
20+
.addTranslate(text, ..._with.map(String))
21+
.buildJson();
22+
this.#player.runCommand("titleraw @s actionbar " + rawText);
23+
}
24+
}

src/tag.js

Lines changed: 0 additions & 43 deletions
This file was deleted.

src/utils/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// export * from "./object.js";
22
// export * from "./range.js";
33
export * from "./array.js";
4+
// export * from "./print.js";
45
export * from "./options.js";

src/utils/print.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Player } from "../player.js";
2+
3+
4+
export function showMessage(player, text, ..._with) {
5+
new Player(player).showMessage(text, ..._with);
6+
}
7+
8+
export function showActionbar(player, text, ..._with) {
9+
new Player(player).showActionbar(text, ..._with);
10+
}

0 commit comments

Comments
 (0)