diff --git a/index.js b/index.js index 9b00a59..5647a81 100644 --- a/index.js +++ b/index.js @@ -13,6 +13,8 @@ import "./src/commandInitializer.js"; export * from "./src/utils/index.js"; export * from "./src/debug/index.js"; export { Tick } from "./src/tick.js"; +// export { Player } from "./src/player.js"; export { Command } from "./src/command.js"; export { Dimension } from "./src/dimension.js"; +export { Gamemode } from "./src/gamemode.js"; export { RawTextBuilder } from "./src/rawTextBuilder/index.js"; diff --git a/src/gamemode.js b/src/gamemode.js new file mode 100644 index 0000000..fa5adf5 --- /dev/null +++ b/src/gamemode.js @@ -0,0 +1,43 @@ +import { GameMode } from "mojang-minecraft"; + + +class _GameMode { + #checkGameMode(player, mode) { + try { + player.runCommand(`testfor @s[m=${mode}]`); + return true; + } catch { + return false; + } + } + + isCreative(player) { + return this.#checkGameMode(player, "c"); + } + + isSurvival(player) { + return this.#checkGameMode(player, "s"); + } + + isAdventure(player) { + return this.#checkGameMode(player, "a"); + } + + isDefault(player) { + return this.#checkGameMode(player, "d"); + } + + get(player) { + if(this.isCreative(player)) { + return GameMode.creative; + } + if(this.isSurvival(player)) { + return GameMode.survival; + } + if(this.isAdventure(player)) { + return GameMode.adventure; + } + } +} + +export const Gamemode = new _GameMode(); diff --git a/src/player.js b/src/player.js index 275bdc4..ed9b4cf 100644 --- a/src/player.js +++ b/src/player.js @@ -1,55 +1,24 @@ -import { World } from "mojang-minecraft"; -import { Command } from "./command.js"; -import { Tag } from "./tag.js"; -import { mergeObject } from "./object.js"; -import { range } from "./utils/index.js"; - - -export class Player { - #player; - #tagSelector; - #inventory; - - constructor(player) { - mergeObject(this, player); - - this.#player = player; - this.player = player; - this.#tagSelector = Command.selectorBuilder(player.name); - - this.#inventory = player.getComponent("inventory").container; - } - - static getAll() { - return World.getPlayers().map(p => new Player(p)); - } - - get items() { - return range(this.#inventory.size) - .map(i => this.#inventory.getItem(i)); - } - - getAllTags() { - return Tag.getAllTags(this.#tagSelector); - } - - hasTag(tag) { - return Tag.hasTag(this.#tagSelector, tag); - } - - addTag(tag) { - return Tag.addTag(this.#tagSelector, tag); - } - - removeAllTags() { - return Tag.removeAllTags(this.#tagSelector); - } - - removeTag(tag) { - return Tag.removeTag(this.#tagSelector, tag); - } - - toString() { - return this.name; - } -} +import { RawTextBuilder } from "./rawTextBuilder/index.js"; + + +export class Player { + #player; + + constructor(player) { + this.#player = player; + } + + sendRawtext(text, ..._with) { + const rawText = new RawTextBuilder() + .addTranslate(text, ..._with.map(String)) + .buildJson(); + this.#player.runCommand("tellraw @s " + rawText); + } + + showRawtext(text, ..._with) { + const rawText = new RawTextBuilder() + .addTranslate(text, ..._with.map(String)) + .buildJson(); + this.#player.runCommand("titleraw @s actionbar " + rawText); + } +} diff --git a/src/tag.js b/src/tag.js deleted file mode 100644 index ee16b1f..0000000 --- a/src/tag.js +++ /dev/null @@ -1,43 +0,0 @@ -import { Player, Entity } from "mojang-minecraft"; -import { Command } from "./command.js"; - - -export const Tag = new (class { - #TagRegex = /§a(?.*?)§r(?:,\s|$)/g; - - getAllTags(selector) { - const { statusMessage } = Command.run(`tag ${selector} list`); - return [...statusMessage.matchAll(this.#TagRegex)].map(m => m.groups.tag); - } - - hasTag(selector, tag) { - return this.getAllTags(selector).includes(tag); - } - - addTag(selector, tag) { - if(this.hasTag(selector, tag)) return false; - try { - Command.run(`tag ${selector} add "${tag}"`); - return true; - } - catch { - return false; - } - } - - removeAllTags(selector) { - this.getAllTags(selector).forEach(t => this.removeTag(selector, t)); - return true; - } - - removeTag(selector, tag) { - if(!this.hasTag(selector, tag)) return false; - try { - Command.run(`tag ${selector} remove "${tag}"`); - return true; - } - catch { - return false; - } - } -})(); \ No newline at end of file diff --git a/src/utils/index.js b/src/utils/index.js index ac1afd1..2ee4dd9 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -1,4 +1,5 @@ // export * from "./object.js"; // export * from "./range.js"; export * from "./array.js"; +// export * from "./print.js"; export * from "./options.js"; diff --git a/src/utils/print.js b/src/utils/print.js new file mode 100644 index 0000000..6708324 --- /dev/null +++ b/src/utils/print.js @@ -0,0 +1,10 @@ +import { Player } from "../player.js"; + + +export function showMessage(player, text, ..._with) { + new Player(player).showMessage(text, ..._with); +} + +export function showActionbar(player, text, ..._with) { + new Player(player).showActionbar(text, ..._with); +}