Skip to content

Commit eb9bfd9

Browse files
committed
1.20.10 Version Beta
1.0.0 - Beta Release
1 parent 075e0c9 commit eb9bfd9

File tree

9 files changed

+126
-36
lines changed

9 files changed

+126
-36
lines changed

Diff for: behavior_pack/functions/disable.mcfunction

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
scriptevent con:terminal_disable

Diff for: behavior_pack/functions/enable.mcfunction

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
scriptevent con:terminal_enable

Diff for: behavior_pack/manifest.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
"format_version": 2,
33
"header": {
44
"name": "Con JS Terminal",
5-
"description": "Terminal with JS input by ConMaster2112",
5+
"description": "Terminal with JS prompts by ConMaster2112",
66
"uuid": "4997eb94-8968-4158-864b-74f1999192f7",
7-
"version": "0.1.1-alpha",
7+
"version": "1.0.0-beta",
88
"min_engine_version": [1, 20, 0]
99
},
1010
"modules": [
@@ -16,7 +16,7 @@
1616
}
1717
],
1818
"dependencies": [
19-
{"module_name": "@minecraft/server","version": "1.4.0-beta"},
19+
{"module_name": "@minecraft/server","version": "1.3.0-beta"},
2020
{"module_name": "@minecraft/server-ui","version": "1.1.0-beta"},
2121
{"module_name": "@minecraft/server-gametest","version": "1.0.0-beta"}
2222
],

Diff for: behavior_pack/scripts/con-terminal.d.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export declare enum OutputType {
2+
SyntaxError = 0,
3+
Error = 1,
4+
Successfull = 2
5+
}
6+
export declare enum LogTypes {
7+
log = 0,
8+
error = 1,
9+
warn = 2
10+
}
11+
export declare function TerminalInput<s>(source: s, message: string, o?: (this: s, type: LogTypes, ...params: any[]) => void): Promise<{
12+
type: OutputType;
13+
value: any;
14+
formatView: string;
15+
multicommand: boolean;
16+
startTime: number;
17+
}>;

Diff for: behavior_pack/scripts/con-terminal.js

+34-12
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const MinecraftModules = [
2626
{ module_name: "@minecraft/server-editor", tag: "editor" },
2727
{ module_name: "@minecraft/server-editor-bindings", tag: "bindings" }
2828
];
29-
var OutputType;
29+
export var OutputType;
3030
(function (OutputType) {
3131
OutputType[OutputType["SyntaxError"] = 0] = "SyntaxError";
3232
OutputType[OutputType["Error"] = 1] = "Error";
@@ -47,14 +47,14 @@ function formatView(type, object) {
4747
return getView[typeof object](ViewStyle.Full, object);
4848
}
4949
export async function TerminalInput(source, message, o = consoleLike) {
50-
const a = await RunCode(message, true, { console: { log: o.bind(source, LogTypes.log), warn: o.bind(source, LogTypes.warn), error: o.bind(source, LogTypes.error) }, print: o.bind(source, LogTypes.log), self: source, setTimeout, setInterval, clearInterval: clearRun, clearTimeout: clearRun });
51-
const multicommand = a.multicommand;
50+
const a = await RunCode(message, true, { console: { log: o.bind(source, LogTypes.log), Map, Set, warn: o.bind(source, LogTypes.warn), error: o.bind(source, LogTypes.error) }, print: o.bind(source, LogTypes.log), self: source, setTimeout, setInterval, clearInterval: clearRun, clearTimeout: clearRun });
51+
const { multicommand, startTime } = a;
5252
if (a.syntaxError)
53-
return { type: OutputType.SyntaxError, value: a.syntaxError, formatView: formatView(OutputType.SyntaxError, a.syntaxError), multicommand };
53+
return { type: OutputType.SyntaxError, value: a.syntaxError, formatView: formatView(OutputType.SyntaxError, a.syntaxError), multicommand, startTime };
5454
const output = await a.promise;
55-
if (typeof output === "object" && symbolError in output)
56-
return { type: OutputType.Error, value: output[symbolError], formatView: formatView(OutputType.Error, output[symbolError]), multicommand };
57-
return { type: OutputType.Successfull, value: output, formatView: formatView(OutputType.Successfull, output), multicommand };
55+
if (typeof output === "object" && output !== null && symbolError in output)
56+
return { type: OutputType.Error, value: output[symbolError], formatView: formatView(OutputType.Error, output[symbolError]), multicommand, startTime };
57+
return { type: OutputType.Successfull, value: output, formatView: formatView(OutputType.Successfull, output), multicommand, startTime };
5858
}
5959
async function RunCode(code, useModules = true, ...scopes) {
6060
let func, output = { syntaxError: undefined, promise: undefined, multicommand: false };
@@ -68,12 +68,13 @@ async function RunCode(code, useModules = true, ...scopes) {
6868
return output;
6969
}
7070
output.multicommand = func.multicommand ?? false;
71+
output.startTime = Date.now();
7172
output.promise = Promise.resolve(func()).catch(er => ({ [symbolError]: er }));
7273
return output;
7374
}
7475
async function BuildAPIScope(...modules) {
7576
let promises = [];
76-
modules.forEach(m => promises.push(import(m.module_name).catch(() => ({}))));
77+
modules.forEach(m => promises.push(import(m.module_name).catch(() => null)));
7778
const dlls = await Promise.all(promises);
7879
return dlls.map((m, i) => ({ [modules[i].tag]: m }));
7980
}
@@ -112,7 +113,22 @@ const getView = {
112113
else if (style === ViewStyle.Short) {
113114
const names = Object.getOwnPropertyNames(any), symbols = Object.getOwnPropertySymbols(any);
114115
//@ts-ignore
115-
const keys = names.map(k => `§7${k}§r§7: ${getView[typeof any[k]](ViewStyle.Primitive, any[k])}§r`).concat(symbols.map(s => `§r${getView["symbol"](ViewStyle.Primitive, s)}§r§7: ${getView[typeof any[s]](ViewStyle.Primitive, any[s])}`));
116+
const keys = names.map(k => {
117+
try {
118+
return `§7${k}§r§7: ${getView[typeof any[k]](ViewStyle.Primitive, any[k])}§r`;
119+
}
120+
catch (error) {
121+
return `§7${k}§r§7: §o(...)§r`;
122+
}
123+
}).concat(symbols.map(s => {
124+
try {
125+
return `§r${getView["symbol"](ViewStyle.Primitive, s)}§r§7: ${getView[typeof any[s]](ViewStyle.Primitive, any[s])}`;
126+
}
127+
catch (error) {
128+
return `§r${getView["symbol"](ViewStyle.Primitive, s)}§r§7: §o(...)§r`;
129+
}
130+
}));
131+
Object.getOwnPropertyDescriptor;
116132
const realKeys = keys.slice(0, 5), typeOf = getTypeOfObject(any);
117133
return `§7${(typeOf == "Object" || typeOf == '') ? "" : typeOf + " "}{${realKeys.join("§7, ")}${keys.length > 5 ? "§r§7, ..." : "§r§7"}}`;
118134
}
@@ -162,11 +178,17 @@ function buildCompoudView(array, base, object, offSet = " ", func = false, deep
162178
}
163179
else {
164180
if (get != undefined) {
165-
const v = get.call(base);
166-
array.push(`${off}§r§7get§r ${typeof key == "string" ? key : getView["symbol"](ViewStyle.Primitive, key)}§7: §r${getView[typeof v](ViewStyle.Short, v)}§r`);
181+
let v;
182+
try {
183+
v = get.call(base);
184+
array.push(`${off}§r§7get§r ${typeof key == "string" ? key : getView["symbol"](ViewStyle.Primitive, key)}§7: §r${getView[typeof v](ViewStyle.Short, v)}§r`);
185+
}
186+
catch (error) {
187+
array.push(`${off}§r§7get§r ${typeof key == "string" ? key : getView["symbol"](ViewStyle.Primitive, key)}§7: §o(...)§r`);
188+
}
167189
}
168190
if (set != undefined)
169-
array.push(`${off}§r§7set§r ${typeof key == "string" ? key : getView["symbol"](ViewStyle.Primitive, key)}§7: (...)§r`);
191+
array.push(`${off}§r§7set§r ${typeof key == "string" ? key : getView["symbol"](ViewStyle.Primitive, key)}§7: §o(...)§r`);
170192
}
171193
}
172194
if (func)

Diff for: behavior_pack/scripts/index.js

+36-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,40 @@
1-
import { world } from "@minecraft/server";
2-
import { TerminalInput } from "./con-terminal";
1+
import { DynamicPropertiesDefinition, MessageSourceType, MinecraftEntityTypes, system, world } from "@minecraft/server";
2+
import { OutputType, TerminalInput } from "./con-terminal";
3+
4+
const propertyName = "allow_terminal";
35

46
world.beforeEvents.chatSend.subscribe(async (ev) => {
5-
const { sender, message } = ev;
6-
if (message.startsWith('>') && sender.isOp()) {
7+
const { sender, message } = ev, reg = message.match(/^( +|)>( +|)/g);
8+
if (reg !== null && sender.isOp() && sender.getDynamicProperty(propertyName)) {
79
ev.cancel = true;
8-
await null;
9-
const out = await TerminalInput(sender, message.substring(1));
10-
sender.sendMessage(out.formatView);
10+
const code = message.substring(reg[0].length);
11+
const task = TerminalInput(sender, code).catch(er=>console.error(er,er.stack));
12+
sender.sendMessage(" §l§h> §r§7" + code);
13+
const out = await task;
14+
switch (out.type) {
15+
case OutputType.Successfull:
16+
case 2:
17+
return sender.sendMessage(" §l§q< §r" + out.formatView.replaceAll("\n", "\n "));
18+
case OutputType.SyntaxError:
19+
case 0:
20+
return sender.sendMessage(" §l§4> §6" + out.value.message);
21+
case OutputType.Error:
22+
case 1:
23+
return sender.sendMessage(" §l§6< §7" + out.value);
24+
default:
25+
break;
26+
}
27+
}
28+
});
29+
world.afterEvents.worldInitialize.subscribe(ev=>ev.propertyRegistry.registerEntityTypeDynamicProperties(new DynamicPropertiesDefinition().defineBoolean(propertyName,false),MinecraftEntityTypes.player))
30+
system.events.scriptEventReceive.subscribe(ev=>{
31+
if(ev.sourceType !== MessageSourceType.clientScript || ev.sourceEntity == null) return;
32+
if(ev.id === "con:terminal_enable") {
33+
ev.sourceEntity.setDynamicProperty(propertyName, true);
34+
ev.sourceEntity.sendMessage("You are now able to use terminal.");
35+
}
36+
if(ev.id === "con:terminal_disable") {
37+
ev.sourceEntity.setDynamicProperty(propertyName, false);
38+
ev.sourceEntity.sendMessage("You are no longer able to use terminal.");
1139
}
12-
});
40+
},{namespaces:["con"]});

Diff for: con-terminal.ts

+31-12
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const MinecraftModules = [
2727
{module_name: "@minecraft/server-editor", tag: "editor"},
2828
{module_name: "@minecraft/server-editor-bindings", tag: "bindings"}
2929
];
30-
enum OutputType{
30+
export enum OutputType{
3131
SyntaxError,
3232
Error,
3333
Successfull
@@ -46,14 +46,14 @@ function formatView(type: OutputType, object: any): string{
4646
return getView[typeof object](ViewStyle.Full, object);
4747
}
4848
export async function TerminalInput<s>(source: s, message: string, o:(this: s, type: LogTypes,...params: any[])=>void = consoleLike){
49-
const a = await RunCode(message, true, {console:{log:o.bind(source,LogTypes.log),warn:o.bind(source,LogTypes.warn),error:o.bind(source,LogTypes.error)},print:o.bind(source,LogTypes.log), self:source, setTimeout, setInterval, clearInterval:clearRun, clearTimeout:clearRun});
50-
const multicommand = a.multicommand;
51-
if(a.syntaxError) return {type: OutputType.SyntaxError, value: a.syntaxError, formatView: formatView(OutputType.SyntaxError, a.syntaxError),multicommand};
49+
const a = await RunCode(message, true, {console:{log:o.bind(source,LogTypes.log),Map,Set,warn:o.bind(source,LogTypes.warn),error:o.bind(source,LogTypes.error)},print:o.bind(source,LogTypes.log), self:source, setTimeout, setInterval, clearInterval:clearRun, clearTimeout:clearRun});
50+
const {multicommand, startTime} = a;
51+
if(a.syntaxError) return {type: OutputType.SyntaxError, value: a.syntaxError, formatView: formatView(OutputType.SyntaxError, a.syntaxError),multicommand, startTime};
5252
const output = await a.promise;
53-
if(typeof output === "object" && symbolError in output) return {type: OutputType.Error, value: output[symbolError], formatView: formatView(OutputType.Error, output[symbolError]),multicommand};
54-
return {type: OutputType.Successfull, value: output, formatView: formatView(OutputType.Successfull, output),multicommand};
53+
if(typeof output === "object" && output !== null && symbolError in output) return {type: OutputType.Error, value: output[symbolError], formatView: formatView(OutputType.Error, output[symbolError]),multicommand, startTime};
54+
return {type: OutputType.Successfull, value: output, formatView: formatView(OutputType.Successfull, output),multicommand, startTime};
5555
}
56-
async function RunCode(code: string, useModules = true, ...scopes: any[]): Promise<{syntaxError?:any, promise?: Promise<any | never>, multicommand: boolean}> {
56+
async function RunCode(code: string, useModules = true, ...scopes: any[]): Promise<{syntaxError?:any, promise?: Promise<any | never>, multicommand: boolean, startTime: number}> {
5757
let func, output: any = {syntaxError: undefined, promise: undefined, multicommand: false};
5858
const modules = useModules?(await BuildAPIScope(...MinecraftModules)):[];
5959
try {
@@ -64,12 +64,13 @@ async function RunCode(code: string, useModules = true, ...scopes: any[]): Promi
6464
return output;
6565
}
6666
output.multicommand = func.multicommand??false;
67+
output.startTime = Date.now();
6768
output.promise = Promise.resolve(func()).catch(er=>({[symbolError]: er}));
6869
return output;
6970
}
7071
async function BuildAPIScope(...modules: {module_name:string, tag:string}[]){
7172
let promises: Promise<any>[] = [];
72-
modules.forEach(m=>promises.push(import(m.module_name).catch(()=>({}))));
73+
modules.forEach(m=>promises.push(import(m.module_name).catch(()=>null)));
7374
const dlls = await Promise.all(promises);
7475
return dlls.map((m,i)=>({[modules[i].tag]: m}));
7576
}
@@ -105,7 +106,20 @@ const getView: {[key: string]:(style: ViewStyle, n: any)=>string} = {
105106
else if (style === ViewStyle.Short){
106107
const names = Object.getOwnPropertyNames(any), symbols = Object.getOwnPropertySymbols(any);
107108
//@ts-ignore
108-
const keys = names.map(k=>`§7${k}§r§7: ${getView[typeof any[k]](ViewStyle.Primitive, any[k])}§r`).concat(symbols.map(s=>`§r${getView["symbol"](ViewStyle.Primitive,s)}§r§7: ${getView[typeof any[s]](ViewStyle.Primitive,any[s])}`));
109+
const keys = names.map(k=>{
110+
try {
111+
return `§7${k}§r§7: ${getView[typeof any[k]](ViewStyle.Primitive, any[k])}§r`;
112+
} catch (error) {
113+
return `§7${k}§r§7: §o(...)§r`;
114+
}
115+
}).concat(symbols.map(s=>{
116+
try {
117+
return `§r${getView["symbol"](ViewStyle.Primitive,s)}§r§7: ${getView[typeof any[s]](ViewStyle.Primitive,any[s])}`;
118+
} catch (error) {
119+
return `§r${getView["symbol"](ViewStyle.Primitive,s)}§r§7: §o(...)§r`;
120+
}
121+
}));
122+
Object.getOwnPropertyDescriptor
109123
const realKeys = keys.slice(0,5), typeOf = getTypeOfObject(any);
110124
return `§7${(typeOf == "Object" || typeOf == '')?"":typeOf + " "}{${realKeys.join("§7, ")}${keys.length>5?"§r§7, ...":"§r§7"}}`;
111125
}
@@ -149,10 +163,15 @@ function buildCompoudView(array: string[], base: any, object: any, offSet = " "
149163
array.push(`${off}§r${typeof key == "string"?key:getView["symbol"](ViewStyle.Primitive, key)}§7: §r${getView[typeof value](ViewStyle.Short,value)}§r`);
150164
}else{
151165
if(get != undefined){
152-
const v = get.call(base);
153-
array.push(`${off}§r§7get§r ${typeof key == "string"?key:getView["symbol"](ViewStyle.Primitive, key)}§7: §r${getView[typeof v](ViewStyle.Short,v)}§r`);
166+
let v;
167+
try {
168+
v = get.call(base);
169+
array.push(`${off}§r§7get§r ${typeof key == "string"?key:getView["symbol"](ViewStyle.Primitive, key)}§7: §r${getView[typeof v](ViewStyle.Short,v)}§r`);
170+
} catch (error) {
171+
array.push(`${off}§r§7get§r ${typeof key == "string"?key:getView["symbol"](ViewStyle.Primitive, key)}§7: §o(...)§r`);
172+
}
154173
}
155-
if(set != undefined) array.push(`${off}§r§7set§r ${typeof key == "string"?key:getView["symbol"](ViewStyle.Primitive, key)}§7: (...)§r`);
174+
if(set != undefined) array.push(`${off}§r§7set§r ${typeof key == "string"?key:getView["symbol"](ViewStyle.Primitive, key)}§7: §o(...)§r`);
156175
}
157176
}
158177
if(func) array.push(`${off}§7[[Native Function]]: §7${getView["boolean"](ViewStyle.Primitive, object.toString().endsWith(nativeFunction))}§r`);

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@
1717
},
1818
"homepage": "https://github.com/Con-JS-Development/Con-Terminal#readme",
1919
"dependencies": {
20-
"@minecraft/server": "^1.3.0-beta.1.20.0-preview.22"
20+
"@minecraft/server": "^1.4.0-beta.1.20.10-preview.20"
2121
}
2222
}

Diff for: tsconfig.json

+2
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@
99
"baseUrl": ".",
1010
"skipLibCheck": true,
1111
"outDir": "./behavior_pack/scripts",
12+
"declaration": true,
13+
"declarationDir": "./behavior_pack/scripts"
1214
}
1315
}

0 commit comments

Comments
 (0)