Skip to content

Permissions improvements #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion behavior_pack/functions/disable.mcfunction

This file was deleted.

1 change: 0 additions & 1 deletion behavior_pack/functions/enable.mcfunction

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
scriptevent con:terminal_all_players_disable
tell @s All players terminal mode is disabled.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
scriptevent con:terminal_all_players_enable
tell @s All players terminal mode is enabled.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
scriptevent con:terminal_allow_blocks_enable
tell @s Allow blocks disabled.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
scriptevent con:terminal_allow_blocks_enable
tell @s Allow blocks enabled.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
scriptevent con:terminal_allow_entities_disable
tell @s Entities are no longer able to run scripts.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
scriptevent con:terminal_allow_entities_enable
tell @s Entities are able to run scripts.
2 changes: 2 additions & 0 deletions behavior_pack/functions/terminal/player_allow.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
scriptevent con:terminal_player_enable
tell @s You are able to use terminal, but OP level could be required.
2 changes: 2 additions & 0 deletions behavior_pack/functions/terminal/player_deny.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
scriptevent con:terminal_player_disable
tell @s You are no longer able to use terminal, unless all_players mode is enabled
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
scriptevent con:terminal_require_op_disable
tell @s OP level is no longer required for using Terminal
2 changes: 2 additions & 0 deletions behavior_pack/functions/terminal/require_op_enable.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
scriptevent con:terminal_require_op_enable
tell @s OP level is now required for using Terminal
2 changes: 2 additions & 0 deletions behavior_pack/functions/terminal/terminal_disable.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
scriptevent con:terminal_disable
tell @s Terminal Disabled
2 changes: 2 additions & 0 deletions behavior_pack/functions/terminal/terminal_enable.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
scriptevent con:terminal_enable
tell @s Terminal Enabled
18 changes: 11 additions & 7 deletions behavior_pack/scripts/con-terminal.d.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
export declare enum OutputType {
declare enum OutputType {
SyntaxError = 0,
Error = 1,
Successfull = 2
}
export declare enum LogTypes {
log = 0,
error = 1,
warn = 2
}
export declare function TerminalInput<s>(source: s, message: string, scope?: never[], o?: (this: s, type: LogTypes, ...params: any[]) => void): Promise<{
declare function formatView(object: any, viewStyle?: ViewStyle): string;
declare function TerminalInput<s>(source: s, message: string, scope?: never[], console?: any): Promise<{
type: OutputType;
value: any;
formatView: string;
multicommand: boolean;
startTime: number;
}>;
declare enum ViewStyle {
Primitive = 0,
Short = 1,
Full = 2,
Infinite = 3
}
declare function timeoutsSupported(): Promise<boolean>;
export { OutputType, formatView as FormatView, TerminalInput, timeoutsSupported };
180 changes: 71 additions & 109 deletions behavior_pack/scripts/con-terminal.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { system } from "@minecraft/server";
const symbolError = Symbol("RunError");
const AsyncFunctionConstructor = (async function () { }).constructor;
const GeneratorFunctionConstructor = (function* () { }).constructor;
const AsyncGeneratorFunctionConstructor = (async function* () { }).constructor;
const runInterval = system.runInterval.bind(system);
const runTimeout = system.runTimeout.bind(system);
const clearRun = system.clearRun.bind(system);
const nativeFunction = "{\n [native code]\n}";
const safePrototypes = [
Object.prototype,
Expand All @@ -26,35 +22,41 @@ const MinecraftModules = [
{ module_name: "@minecraft/server-editor", tag: "editor" },
{ module_name: "@minecraft/server-editor-bindings", tag: "bindings" }
];
export var OutputType;
var OutputType;
(function (OutputType) {
OutputType[OutputType["SyntaxError"] = 0] = "SyntaxError";
OutputType[OutputType["Error"] = 1] = "Error";
OutputType[OutputType["Successfull"] = 2] = "Successfull";
})(OutputType || (OutputType = {}));
export var LogTypes;
var LogTypes;
(function (LogTypes) {
LogTypes[LogTypes["log"] = 0] = "log";
LogTypes[LogTypes["error"] = 1] = "error";
LogTypes[LogTypes["warn"] = 2] = "warn";
LogTypes[LogTypes["info"] = 3] = "info";
})(LogTypes || (LogTypes = {}));
const setInterval = (callBack, interval = 0, ...params) => runInterval(() => callBack(...params), interval);
const setTimeout = (callBack, interval = 0, ...params) => runTimeout(() => callBack(...params), interval);
const consoleTamplate = {
log: consoleLike.bind(null, LogTypes.log),
warn: consoleLike.bind(null, LogTypes.warn),
error: consoleLike.bind(null, LogTypes.error),
info: consoleLike.bind(null, LogTypes.info)
};
const runsTemplate = {};
function consoleLike(type, ...texts) {
console.warn(`[Terminal-${LogTypes[type]}]: `, ...texts.map(a => getView[typeof a](ViewStyle.Short, a)));
}
function formatView(type, object) {
return getView[typeof object](ViewStyle.Full, object);
function formatView(object, viewStyle = ViewStyle.Full) {
return getView[typeof object](viewStyle, object);
}
export async function TerminalInput(source, message, scope = [], o = consoleLike) {
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 }, ...scope);
async function TerminalInput(source, message, scope = [], console = consoleTamplate) {
const a = await RunCode(message, true, { console: { ...console }, print: consoleLike.bind(null, LogTypes.log), self: source }, runsTemplate, ...scope);
const { multicommand, startTime } = a;
if (a.syntaxError)
return { type: OutputType.SyntaxError, value: a.syntaxError, formatView: formatView(OutputType.SyntaxError, a.syntaxError), multicommand, startTime };
return { type: OutputType.SyntaxError, value: a.syntaxError, formatView: formatView(a.syntaxError), multicommand, startTime };
const output = await a.promise;
if (typeof output === "object" && output !== null && symbolError in output)
return { type: OutputType.Error, value: output[symbolError], formatView: formatView(OutputType.Error, output[symbolError]), multicommand, startTime };
return { type: OutputType.Successfull, value: output, formatView: formatView(OutputType.Successfull, output), multicommand, startTime };
return { type: OutputType.Error, value: output[symbolError], formatView: formatView(output[symbolError]), multicommand, startTime };
return { type: OutputType.Successfull, value: output, formatView: formatView(output), multicommand, startTime };
}
async function RunCode(code, useModules = true, ...scopes) {
let func, output = { syntaxError: undefined, promise: undefined, multicommand: false };
Expand Down Expand Up @@ -101,6 +103,7 @@ var ViewStyle;
ViewStyle[ViewStyle["Primitive"] = 0] = "Primitive";
ViewStyle[ViewStyle["Short"] = 1] = "Short";
ViewStyle[ViewStyle["Full"] = 2] = "Full";
ViewStyle[ViewStyle["Infinite"] = 3] = "Infinite";
})(ViewStyle || (ViewStyle = {}));
const getView = {
"object"(style, any) {
Expand Down Expand Up @@ -133,7 +136,7 @@ const getView = {
return `§7${(typeOf == "Object" || typeOf == '') ? "" : typeOf + " "}{${realKeys.join("§7, ")}${keys.length > 5 ? "§r§7, ..." : "§r§7"}}`;
}
else {
return getView["object"](ViewStyle.Short, any) + "\n" + buildCompoudView([], any, any).join('\n');
return getView["object"](ViewStyle.Short, any) + "\n" + buildCompoudView(style, [], any, any).join('\n');
}
},
"function"(n, any) {
Expand All @@ -149,15 +152,15 @@ const getView = {
else if (n === ViewStyle.Primitive)
return "§5§oƒ§r";
else
return getView["object"](ViewStyle.Short, any) + "\n" + buildCompoudView([], any, any, " ", true).join('\n');
return getView["function"](ViewStyle.Short, any) + "\n" + buildCompoudView(n, [], any, any, " ", true).join('\n');
},
"number"(n, any) { return `§3${any.toString()}§r`; },
"bigint"(n, any) { return `§3${any.toString()}§r`; },
"boolean"(n, any) { return `§3${any.toString()}§r`; },
"symbol"(n, any) { return `§7Symbol(${any.description})§r`; },
"undefined"() { return "§7§oundefined§r"; },
"string"(n, any) {
if (n === ViewStyle.Full)
if (n === ViewStyle.Full || n == ViewStyle.Infinite)
return `§3"${any}"§r`;
else if (n === ViewStyle.Short)
return `§3"${any.split('\n')[0]}"§r`;
Expand All @@ -167,21 +170,37 @@ const getView = {
}
}
};
function buildCompoudView(array, base, object, offSet = " ", func = false, deepth = 1) {
function buildCompoudView(viewStyle, array, base, object, offSet = " ", func = false, deepth = 1, knownSets = []) {
const off = offSet.repeat(deepth);
const prototype = Object.getPrototypeOf(object);
const descriptors = Object.getOwnPropertyDescriptors(object);
knownSets.push(object);
for (const key of [...Object.getOwnPropertyNames(descriptors), ...Object.getOwnPropertySymbols(descriptors)]) {
const { value, set, get } = descriptors[key];
if (value) {
array.push(`${off}§r${typeof key == "string" ? key : getView["symbol"](ViewStyle.Primitive, key)}§7: §r${getView[typeof value](ViewStyle.Short, value)}§r`);
const desc = descriptors[key], { value, set, get } = desc;
if ("value" in desc) {
//@ts-ignore
if (viewStyle != ViewStyle.Infinite || typeof value !== "object")
array.push(`${off}§r${typeof key == "string" ? key : getView["symbol"](ViewStyle.Primitive, key)}§7: §r${getView[typeof value](ViewStyle.Short, value)}§r`);
else if (knownSets.includes(value))
array.push(`${off}§r${typeof key == "string" ? key : getView["symbol"](ViewStyle.Primitive, key)}§7: §c::Recursive:: §r${getView[typeof value](ViewStyle.Short, value)}§r`);
else {
array.push(`${off}§r${typeof key == "string" ? key : getView["symbol"](ViewStyle.Primitive, key)}§7: §r`);
buildCompoudView(viewStyle, array, value, value, offSet, typeof value === "function", deepth + 1, [...knownSets]);
}
}
else {
if (get != undefined) {
let v;
try {
v = get.call(base);
array.push(`${off}§r§7get§r ${typeof key == "string" ? key : getView["symbol"](ViewStyle.Primitive, key)}§7: §r${getView[typeof v](ViewStyle.Short, v)}§r`);
const v = get.call(base);
//@ts-ignore
if (viewStyle != ViewStyle.Infinite || typeof v !== "object")
array.push(`${off}§r§7get§r ${typeof key == "string" ? key : getView["symbol"](ViewStyle.Primitive, key)}§7: §r${getView[typeof v](ViewStyle.Short, v)}§r`);
else if (knownSets.includes(v))
array.push(`${off}§r§7get§r ${typeof key == "string" ? key : getView["symbol"](ViewStyle.Primitive, key)}§7: §c::Recursive:: §r${getView[typeof v](ViewStyle.Short, v)}§r`);
else {
array.push(`${off}§r§7get§r ${typeof key == "string" ? key : getView["symbol"](ViewStyle.Primitive, key)}§7:§r`);
buildCompoudView(viewStyle, array, v, v, offSet, typeof v === "function", deepth + 1, [...knownSets]);
}
}
catch (error) {
array.push(`${off}§r§7get§r ${typeof key == "string" ? key : getView["symbol"](ViewStyle.Primitive, key)}§7: §o(...)§r`);
Expand All @@ -196,94 +215,37 @@ function buildCompoudView(array, base, object, offSet = " ", func = false, deep
if (prototype == null)
return array;
const typOf = getTypeOfObject(prototype);
array.push(`${off}§7[[Prototype]]: ${(typOf == "" ? "Object" : typOf)}§r`);
if (!safePrototypes.includes(prototype))
buildCompoudView(array, base, prototype, offSet, typeof prototype === 'function', deepth + 1);
return array;
}
/*
export function toStringPrimitiveFull(any){
switch (typeof any) {
case 'object':
if(any === null){
return "§7§onull";
}else if (any instanceof Error) {
return `§6${any}\n${any.stack}`;
}else{
const base = any, names = Object.getOwnPropertyNames(base), symbols = Object.getOwnPropertySymbols(base);
const keys = names.filter(a=>(base.__lookupGetter__?.(a) == undefined && base.__lookupSetter__?.(a) == undefined)).map(k=>`§7${k}§r§7: ${toStringPrimitiveShort(base[k])}§r`).concat(symbols.map(s=>`§r${toStringPrimitiveShort(s)}§r§7: ${toStringPrimitiveShort(base[s])}`));
const realKeys = keys.slice(0,5), typeOf = getTypeOfObject(base);
let output = `§7${(typeOf == "Object" || typeOf == '')?"":typeOf + " "}{${realKeys.join("§7, ")}${keys.length>5?"§r§7, ...":"§r§7"}}§r`;
function buildLines(base, offSet = " "){
const prototype = Object.getPrototypeOf(base);
for (const keyName of Object.getOwnPropertyNames(base)) {
let getter = base.__lookupGetter__?.(keyName);
let setter = base.__lookupSetter__?.(keyName);
if(getter == undefined&&setter == undefined){
output += `\n${offSet}§r${keyName}§7: §r${toStringPrimitive(base[keyName])}`;
} else {
if(getter != undefined) output += `\n${offSet}§7get§r ${keyName}§7: (...)`;
if(setter != undefined) output += `\n${offSet}§7set§r ${keyName}§7: (...)`;
}
}
for (const keySymbol of Object.getOwnPropertySymbols(base)){
let getter = base.__lookupGetter__?.(keySymbol);
let setter = base.__lookupSetter__?.(keySymbol);
if(getter == undefined&&setter == undefined){
output += `\n${offSet}§r${toStringPrimitiveShort(keySymbol)}§7: §r${toStringPrimitive(base[keySymbol])}`;
} else {
if(getter != undefined) output += `\n${offSet}§7get§r ${toStringPrimitiveShort(keySymbol)}§7: (...)`;
if(setter != undefined) output += `\n${offSet}§7set§r ${toStringPrimitiveShort(keySymbol)}§7: (...)`;
}
}
if(prototype != null){
const typOf = getTypeOfObject(prototype);
output += `\n${offSet}§r[[Prototype]]§r§7: ` + (typOf==""?"Object":typOf)
if(prototype != Object.prototype && prototype != Array.prototype && prototype != Map.prototype){
buildLines(prototype, offSet + " ");
}
}
}
buildLines(base);
return output;
}
case 'function': return any.toString();
case 'symbol': return `§7Symbol(${any.description})`;
case 'bigint':
case 'number':
case 'boolean': return `§3${any.toString()}§r`;
case 'undefined': return "§7§oundefined";
case 'string': return `§3"${any}"§r`;
default:
break;
if (!safePrototypes.includes(prototype)) {
array.push(`${off}§7[[Prototype]]: ${(typOf == "" ? "Object" : typOf)}§r`);
buildCompoudView(viewStyle, array, base, prototype, offSet, typeof prototype === 'function', deepth + 1, [...knownSets]);
}
else
array.push(`${off}§7[[Prototype]]: ${(typOf == "" ? "Object" : typOf)} §r${getView[typeof prototype](ViewStyle.Short, prototype)}§r`);
return array;
}
export function toStringPrimitive(any: any){
switch (typeof any) {
case 'object':
if(any === null){
return "§7§onull§r";
}else if (any instanceof Error) {
return `§6${any}§r`;
}else{
const base = any, names = Object.getOwnPropertyNames(base), symbols = Object.getOwnPropertySymbols(base);
const keys = names.map(k=>`§7${k}§r§7: ${toStringPrimitiveShort(base[k])}§r`).concat(symbols.map(s=>`§r${toStringPrimitiveShort(s)}§r§7: ${toStringPrimitiveShort(base[s])}`));
const realKeys = keys.slice(0,5), typeOf = getTypeOfObject(base);
return `§7${(typeOf == "Object" || typeOf == '')?"":typeOf + " "}{${realKeys.join("§7, ")}${keys.length>5?"§r§7, ...":"§r§7"}}`;
}
case 'function': return toFunctionString(any);
case 'symbol': return `Symbol(${any.description})`;
case 'bigint':
case 'number':
case 'boolean': return `§3${any.toString()}§r`;
case 'undefined': return "§7§oundefined";
case 'string': return `§6"${any}"§r`;
default:
break;
}
}*/
function getTypeOfObject(obj) { return (obj[Symbol.toStringTag] ?? ((typeof obj === "function" ? obj.name : undefined) ?? ((obj.constructor?.prototype == obj ? obj.constructor?.name : obj.__proto__?.constructor?.name) ?? ""))); }
async function timeoutsInit() {
const a = await import("@minecraft/server").catch(e => null);
if (a === null)
return;
if (a.system?.runTimeout == undefined)
return;
const system = a.system;
const runInterval = system.runInterval.bind(system);
const runTimeout = system.runTimeout.bind(system);
const clearRun = system.clearRun.bind(system);
runsTemplate.setInterval = (callBack, interval = 0, ...params) => runInterval(() => callBack(...params), interval);
runsTemplate.setTimeout = (callBack, interval = 0, ...params) => runTimeout(() => callBack(...params), interval);
runsTemplate.clearInterval = clearRun;
runsTemplate.clearTimeout = clearRun;
}
async function timeoutsSupported() {
const a = await import("@minecraft/server").catch(e => { });
return a?.system?.runTimeout !== undefined;
}
//@ts-ignore
globalThis[Symbol.toStringTag] = 'GlobalThis';
//@ts-ignore
globalThis.console[Symbol.toStringTag] = "Console";
timeoutsInit();
export { OutputType, formatView as FormatView, TerminalInput, timeoutsSupported };
Loading