|
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 | +}; |
0 commit comments