diff --git a/index.js b/index.js index b0c5d58..6fae504 100644 --- a/index.js +++ b/index.js @@ -1,251 +1,325 @@ -(function () { - const tasks = []; - const framework = { - base: { - characters: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=', - encode: (string) => { - let index = 0; - let result = ''; - while (index < string.length) { - let a = string.charCodeAt(index++) || 0; - let b = string.charCodeAt(index++) || 0; - let c = string.charCodeAt(index++) || 0; - let d = ((b & 0xf) << 2) | ((c >> 6) & 0x3); - let e = c & 0x3f; - b ? c || (e = 64) : (d = e = 64); - result += framework.base.characters.charAt((a >> 2) & 0x3f); - result += framework.base.characters.charAt(((a & 0x3) << 4) | ((b >> 4) & 0xf)); - result += framework.base.characters.charAt(d); - result += framework.base.characters.charAt(e); +const tasks = []; +const framework = { + access: (object) => { + if (object === null || typeof object !== 'object') { + return object; + } else { + const output = { instance: object }; + Object.entries(object).forEach((entry) => { + if (toString.apply(entry[1]) === '[foreign HostFunction]') { + Object.defineProperty(output, entry[0], { + get () { + const output = (...args) => framework.access(entry[1](...args)); + output.hostFunction = entry[0]; + return output; + } + }); + } else { + Object.defineProperty(output, entry[0], { + get () { + return framework.access(entry[1]); + } + }); } - return result; - }, - decode: (string) => { - let index = 0; - let result = ''; - while (index < string.length) { - let a = framework.base.characters.indexOf(string.charAt(index++)); - let b = framework.base.characters.indexOf(string.charAt(index++)); - let c = framework.base.characters.indexOf(string.charAt(index++)); - let d = framework.base.characters.indexOf(string.charAt(index++)); - let e = ((b & 0xf) << 4) | ((c >> 2) & 0xf); - let f = ((c & 0x3) << 6) | (d & 0x3f); - result += String.fromCharCode(((a & 0x3f) << 2) | ((b >> 4) & 0x3)); - result += e ? String.fromCharCode(e) : ''; - result += f ? String.fromCharCode(f) : ''; + let index = undefined; + entry[0].startsWith('is') && entry[0][2] && (index = 2); + entry[0].startsWith('get') && entry[0][3] && (index = 3); + if (index) { + let key = entry[0].slice(index); + if (key.length) { + let camel = key[0].toLowerCase() + key.slice(1); + if (!Object.keys(object).includes(camel)) { + try { + entry[1](); + Object.defineProperty(output, camel, { + get () { + return framework.access(entry[1]()); + }, + set (value) { + return object[`set${key}`] && object[`set${key}`](value); + } + }); + } catch (error) {} + } + } } - return result; - } - }, - camel: (string, separator) => { - const pascal = framework.pascal(string, separator); - return framework.lower(pascal[0]) + pascal.slice(1); - }, - clamp: (number, min, max) => { - return number < min ? min : number > max ? max : number; - }, - collect: (...array) => { - const output = new java.util.ArrayList(); - array.forEach((entry) => { - output.add(entry); }); - return output; - }, - define: (object, consumer) => { - consumer || - (consumer = (entry) => { - return { - get: () => { - return entry.value; - }, - set: (value) => { - entry.value = value; - } - }; - }); - const output = {}; - framework.entries(object).forEach((entry) => { - const info = consumer(entry); - if (info) { - Object.defineProperty(output, entry.key, { + const array = core.array(object); + Object.keys(array).forEach((index) => { + if (!Object.keys(output).includes(index)) { + Object.defineProperty(output, index, { get () { - if (info.get) { - return info.get(); - } - }, - set (value) { - if (info.set) { - info.set(value); - } + return framework.access(array[index]); } }); } }); return output; - }, - entries: (object) => { - return framework.keys(object).map((key) => { - return { key: key, value: object[key] }; + } + }, + array: (object) => { + const output = []; + if (typeof object.length === 'number') { + if (object.length > 0) { + let index = 0; + while (output.length < object.length) { + output.push(object[index++]); + } + } + } else if (typeof object.forEach === 'function') { + object.forEach((entry) => { + output.push(entry); }); - }, - extend: (object, ...objects) => { - return Object.assign(object, ...objects); - }, - flat: (array) => { - return array.filter((entry) => { - return entry; + } else if (typeof object.forEachRemaining === 'function') { + object.forEachRemaining((entry) => { + output.push(entry); }); - }, - interval: (script, period) => { - const state = { iteration: null }; - const loop = () => { - try { - script(); - } catch (error) { - console.error(error); - } - state.iteration = framework.timeout(loop, period); - }; - state.iteration = framework.timeout(loop, period); - const output = { cancel: () => state.iteration.cancel() }; - tasks.push(output); - return output; - }, - key: (object, value) => { - return framework.keys(object)[framework.values(object).indexOf(value)]; - }, - keys: (object) => { - return Object.keys(object); - }, - lower: (string) => { - return string.toLowerCase(); - }, - match: (object, filter) => { - switch (typeof filter) { - case 'undefined': - return true; - case 'function': - return filter(object); - case 'object': - switch (framework.type(filter)) { - case 'Array': - return filter - .map((entry) => { - return framework.match(object, entry); - }) - .includes(true); - case 'Object': - return !framework - .keys(filter) - .map((key) => { - return framework.match(object[key], filter[key]); - }) - .includes(false); - default: - return object === filter; - } - default: - return object === filter; + } + return output; + }, + base: { + characters: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=', + encode: (string) => { + let index = 0; + let result = ''; + while (index < string.length) { + let a = string.charCodeAt(index++) || 0; + let b = string.charCodeAt(index++) || 0; + let c = string.charCodeAt(index++) || 0; + let d = ((b & 0xf) << 2) | ((c >> 6) & 0x3); + let e = c & 0x3f; + b ? c || (e = 64) : (d = e = 64); + result += framework.base.characters.charAt((a >> 2) & 0x3f); + result += framework.base.characters.charAt(((a & 0x3) << 4) | ((b >> 4) & 0xf)); + result += framework.base.characters.charAt(d); + result += framework.base.characters.charAt(e); } + return result; }, - mirror: (options) => { - options || (options = {}); - const mirror = framework.extend(options.array || [], { - add: options.add || (() => {}), - remove: options.remove || (() => {}), - clear: options.clear || (() => {}) + decode: (string) => { + let index = 0; + let result = ''; + while (index < string.length) { + let a = framework.base.characters.indexOf(string.charAt(index++)); + let b = framework.base.characters.indexOf(string.charAt(index++)); + let c = framework.base.characters.indexOf(string.charAt(index++)); + let d = framework.base.characters.indexOf(string.charAt(index++)); + let e = ((b & 0xf) << 4) | ((c >> 2) & 0xf); + let f = ((c & 0x3) << 6) | (d & 0x3f); + result += String.fromCharCode(((a & 0x3f) << 2) | ((b >> 4) & 0x3)); + result += e ? String.fromCharCode(e) : ''; + result += f ? String.fromCharCode(f) : ''; + } + return result; + } + }, + camel: (string, separator) => { + const pascal = framework.pascal(string, separator); + return framework.lower(pascal[0]) + pascal.slice(1); + }, + clamp: (number, min, max) => { + return number < min ? min : number > max ? max : number; + }, + collect: (...array) => { + const output = new java.util.ArrayList(); + array.forEach((entry) => { + output.add(entry); + }); + return output; + }, + define: (object, consumer) => { + consumer || + (consumer = (entry) => { + return { + get: () => { + return entry.value; + }, + set: (value) => { + entry.value = value; + } + }; }); - return { - get: () => { - return mirror; - }, - set: (value) => { - mirror.clear(); - value.forEach(mirror.add); - } - }; - }, - object: (array, consumer) => { - consumer || - (consumer = (entry, index) => { - return { [entry.key || index]: entry.value || entry }; - }); - return framework.extend({}, ...framework.flat(array.map(consumer))); - }, - pascal: (string, separator) => { - return string - .split(separator) - .map((chunk) => { - return framework.upper(chunk[0]) + framework.lower(chunk.slice(1)); - }) - .join(''); - }, - simplify: (...context) => { - let output = null; - const object = context[0]; - switch (framework.type(object)) { - case 'Array': - output = []; - for (let entry of object) { - if (!context.includes(entry)) { - output.push(framework.simplify(entry, ...context)); + const output = {}; + framework.entries(object).forEach((entry) => { + const info = consumer(entry); + if (info) { + Object.defineProperty(output, entry.key, { + get () { + if (info.get) { + return info.get(); } - } - output = framework.flat(output); - output.length || (output = null); - break; - case 'Object': - output = {}; - for (let key of framework.keys(object)) { - if (!context.includes(object[key])) { - output[key] = framework.simplify(object[key], ...context); + }, + set (value) { + if (info.set) { + info.set(value); } } - output = framework.strain(output); - framework.keys(output).length || (output = null); - break; - default: - output = object; - } - return output; - }, - splice: (string, separator, ...index) => { - return string.split(separator).slice(...index).join(separator); - }, - strain: (object, consumer) => { - consumer || - (consumer = (entry) => { - return entry.value; }); - return framework.object(framework.entries(object).filter(consumer)); - }, - timeout: (script, period) => { - const state = { cancel: false }; - const unit = java.util.concurrent.TimeUnit.MILLISECONDS; - const runnable = new (Java.extend(Java.type('java.lang.Runnable')))({ run: () => state.cancel || script() }); - java.util.concurrent.CompletableFuture.delayedExecutor(period, unit).execute(runnable); - const output = { cancel: () => (state.cancel = true) }; - tasks.push(output); - return output; - }, - type: (object) => { - const type = toString.apply(object).split(' ')[1].slice(0, -1); - return type === 'Object' ? object.constructor.name : type; - }, - upper: (string) => { - return string.toUpperCase(); - }, - uuid: (string) => { - return string ? java.util.UUID.fromString(string) : java.util.UUID.randomUUID(); - }, - values: (object) => { - return Object.values(object); + } + }); + return output; + }, + entries: (object) => { + return framework.keys(object).map((key) => { + return { key: key, value: object[key] }; + }); + }, + extend: (object, ...objects) => { + return Object.assign(object, ...objects); + }, + flat: (array) => { + return array.filter((entry) => { + return entry; + }); + }, + interval: (script, period) => { + const state = { iteration: null }; + const loop = () => { + try { + script(); + } catch (error) { + console.error(error); + } + state.iteration = framework.timeout(loop, period); + }; + state.iteration = framework.timeout(loop, period); + const output = { cancel: () => state.iteration.cancel() }; + tasks.push(output); + return output; + }, + key: (object, value) => { + return framework.keys(object)[framework.values(object).indexOf(value)]; + }, + keys: (object) => { + return Object.keys(object); + }, + lower: (string) => { + return string.toLowerCase(); + }, + match: (object, filter) => { + switch (typeof filter) { + case 'undefined': + return true; + case 'function': + return filter(object); + case 'object': + switch (framework.type(filter)) { + case 'Array': + return filter + .map((entry) => { + return framework.match(object, entry); + }) + .includes(true); + case 'Object': + return !framework + .keys(filter) + .map((key) => { + return framework.match(object[key], filter[key]); + }) + .includes(false); + default: + return object === filter; + } + default: + return object === filter; } - }; + }, + mirror: (options) => { + options || (options = {}); + const mirror = framework.extend(options.array || [], { + add: options.add || (() => {}), + remove: options.remove || (() => {}), + clear: options.clear || (() => {}) + }); + return { + get: () => { + return mirror; + }, + set: (value) => { + mirror.clear(); + value.forEach(mirror.add); + } + }; + }, + object: (array, consumer) => { + consumer || + (consumer = (entry, index) => { + return { [entry.key || index]: entry.value || entry }; + }); + return framework.extend({}, ...framework.flat(array.map(consumer))); + }, + pascal: (string, separator) => { + return string + .split(separator) + .map((chunk) => { + return framework.upper(chunk[0]) + framework.lower(chunk.slice(1)); + }) + .join(''); + }, + simplify: (...context) => { + let output = null; + const object = context[0]; + switch (framework.type(object)) { + case 'Array': + output = []; + for (let entry of object) { + if (!context.includes(entry)) { + output.push(framework.simplify(entry, ...context)); + } + } + output = framework.flat(output); + output.length || (output = null); + break; + case 'Object': + output = {}; + for (let key of framework.keys(object)) { + if (!context.includes(object[key])) { + output[key] = framework.simplify(object[key], ...context); + } + } + output = framework.strain(output); + framework.keys(output).length || (output = null); + break; + default: + output = object; + } + return output; + }, + splice: (string, separator, ...index) => { + return string.split(separator).slice(...index).join(separator); + }, + strain: (object, consumer) => { + consumer || + (consumer = (entry) => { + return entry.value; + }); + return framework.object(framework.entries(object).filter(consumer)); + }, + timeout: (script, period) => { + const state = { cancel: false }; + const runnable = new (Java.extend(Java.type('java.lang.Runnable')))({ run: () => state.cancel || script() }); + server.getScheduler().runTaskLater(core.plugin, runnable, Math.ceil(period / 50)); + const output = { cancel: () => (state.cancel = true) }; + tasks.push(output); + return output; + }, + type: (object) => { + const type = toString.apply(object).split(' ')[1].slice(0, -1); + return type === 'Object' ? object.constructor.name : type; + }, + upper: (string) => { + return string.toUpperCase(); + }, + uuid: (string) => { + return string ? java.util.UUID.fromString(string) : java.util.UUID.randomUUID(); + }, + values: (object) => { + return Object.values(object); + } +}; - core.event('org.bukkit.event.server.PluginDisableEvent', (event) => { - event.getPlugin() === core.plugin && tasks.forEach((task) => task.cancel()); - }); +core.event('org.bukkit.event.server.PluginDisableEvent', (event) => { + event.getPlugin() === core.plugin && tasks.forEach((task) => task.cancel()); +}); - core.export(framework); -})(); +core.export(framework); diff --git a/index.min.js b/index.min.js index 70c42a6..98ef8bd 100644 --- a/index.min.js +++ b/index.min.js @@ -1 +1 @@ -!function(){const tasks=[],framework={array:object=>{const output=[];if("number"==typeof object.length){if(object.length>0){let index=0;for(;output.length{output.push(entry)}):"function"==typeof object.forEachRemaining&&object.forEachRemaining(entry=>{output.push(entry)});return output},base:{characters:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",encode:string=>{let index=0,result="";for(;index>6&3,e=63&c;b?c||(e=64):d=e=64,result+=framework.base.characters.charAt(a>>2&63),result+=framework.base.characters.charAt((3&a)<<4|b>>4&15),result+=framework.base.characters.charAt(d),result+=framework.base.characters.charAt(e)}return result},decode:string=>{let index=0,result="";for(;index>2&15,f=(3&c)<<6|63&framework.base.characters.indexOf(string.charAt(index++));result+=String.fromCharCode((63&a)<<2|b>>4&3),result+=e?String.fromCharCode(e):"",result+=f?String.fromCharCode(f):""}return result}},camel:(string,separator)=>{const pascal=framework.pascal(string,separator);return framework.lower(pascal[0])+pascal.slice(1)},clamp:(number,min,max)=>numbermax?max:number,collect:(...array)=>{const output=new java.util.ArrayList;return array.forEach(entry=>{output.add(entry)}),output},define:(object,consumer)=>{consumer||(consumer=entry=>({get:()=>entry.value,set:value=>{entry.value=value}}));const output={};return framework.entries(object).forEach(entry=>{const info=consumer(entry);info&&Object.defineProperty(output,entry.key,{get(){if(info.get)return info.get()},set(value){info.set&&info.set(value)}})}),output},entries:object=>framework.keys(object).map(key=>({key:key,value:object[key]})),extend:(object,...objects)=>Object.assign(object,...objects),flat:array=>array.filter(entry=>entry),interval:(script,period)=>{const state={iteration:null},loop=()=>{try{script()}catch(error){console.error(error)}state.iteration=framework.timeout(loop,period)};state.iteration=framework.timeout(loop,period);const output={cancel:()=>state.iteration.cancel()};return tasks.push(output),output},key:(object,value)=>framework.keys(object)[framework.values(object).indexOf(value)],keys:object=>Object.keys(object),lower:string=>string.toLowerCase(),match:(object,filter)=>{switch(typeof filter){case"undefined":return!0;case"function":return filter(object);case"object":switch(framework.type(filter)){case"Array":return filter.map(entry=>framework.match(object,entry)).includes(!0);case"Object":return!framework.keys(filter).map(key=>framework.match(object[key],filter[key])).includes(!1);default:return object===filter}default:return object===filter}},mirror:options=>{options||(options={});const mirror=framework.extend(options.array||[],{add:options.add||(()=>{}),remove:options.remove||(()=>{}),clear:options.clear||(()=>{})});return{get:()=>mirror,set:value=>{mirror.clear(),value.forEach(mirror.add)}}},object:(array,consumer)=>(consumer||(consumer=(entry,index)=>({[entry.key||index]:entry.value||entry})),framework.extend({},...framework.flat(framework.array(array).map(consumer)))),pascal:(string,separator)=>string.split(separator).map(chunk=>framework.upper(chunk[0])+framework.lower(chunk.slice(1))).join(""),simplify:(...context)=>{let output=null;const object=context[0];switch(framework.type(object)){case"Array":output=[];for(let entry of object)context.includes(entry)||output.push(framework.simplify(entry,...context));output=framework.flat(output),output.length||(output=null);break;case"Object":output={};for(let key of framework.keys(object))context.includes(object[key])||(output[key]=framework.simplify(object[key],...context));output=framework.strain(output),framework.keys(output).length||(output=null);break;default:output=object}return output},splice:(string,separator,...index)=>string.split(separator).slice(...index).join(separator),strain:(object,consumer)=>(consumer||(consumer=entry=>entry.value),framework.object(framework.entries(object).filter(consumer))),timeout:(script,period)=>{const state={cancel:!1},unit=java.util.concurrent.TimeUnit.MILLISECONDS,runnable=new(Java.extend(Java.type("java.lang.Runnable")))({run:()=>state.cancel||script()});java.util.concurrent.CompletableFuture.delayedExecutor(period,unit).execute(runnable);const output={cancel:()=>state.cancel=!0};return tasks.push(output),output},type:object=>{const type=toString.apply(object).split(" ")[1].slice(0,-1);return"Object"===type?object.constructor.name:type},upper:string=>string.toUpperCase(),uuid:string=>string?java.util.UUID.fromString(string):java.util.UUID.randomUUID(),values:object=>Object.values(object)};core.event("org.bukkit.event.server.PluginDisableEvent",event=>{event.getPlugin()===core.plugin&&tasks.forEach(task=>task.cancel())}),core.export(framework)}(); \ No newline at end of file +const tasks=[],framework={access:object=>{if(null===object||"object"!=typeof object)return object;{const output={instance:object};Object.entries(object).forEach(entry=>{"[foreign HostFunction]"===toString.apply(entry[1])?Object.defineProperty(output,entry[0],{get(){const output=(...args)=>framework.access(entry[1](...args));return output.hostFunction=entry[0],output}}):Object.defineProperty(output,entry[0],{get:()=>framework.access(entry[1])});let index=void 0;if(entry[0].startsWith("is")&&entry[0][2]&&(index=2),entry[0].startsWith("get")&&entry[0][3]&&(index=3),index){let key=entry[0].slice(index);if(key.length){let camel=key[0].toLowerCase()+key.slice(1);if(!Object.keys(object).includes(camel))try{entry[1](),Object.defineProperty(output,camel,{get:()=>framework.access(entry[1]()),set:value=>object[`set${key}`]&&object[`set${key}`](value)})}catch(error){}}}});const array=core.array(object);return Object.keys(array).forEach(index=>{Object.keys(output).includes(index)||Object.defineProperty(output,index,{get:()=>framework.access(array[index])})}),output}},array:object=>{const output=[];if("number"==typeof object.length){if(object.length>0){let index=0;for(;output.length{output.push(entry)}):"function"==typeof object.forEachRemaining&&object.forEachRemaining(entry=>{output.push(entry)});return output},base:{characters:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",encode:string=>{let index=0,result="";for(;index>6&3,e=63&c;b?c||(e=64):d=e=64,result+=framework.base.characters.charAt(a>>2&63),result+=framework.base.characters.charAt((3&a)<<4|b>>4&15),result+=framework.base.characters.charAt(d),result+=framework.base.characters.charAt(e)}return result},decode:string=>{let index=0,result="";for(;index>2&15,f=(3&c)<<6|63&framework.base.characters.indexOf(string.charAt(index++));result+=String.fromCharCode((63&a)<<2|b>>4&3),result+=e?String.fromCharCode(e):"",result+=f?String.fromCharCode(f):""}return result}},camel:(string,separator)=>{const pascal=framework.pascal(string,separator);return framework.lower(pascal[0])+pascal.slice(1)},clamp:(number,min,max)=>numbermax?max:number,collect:(...array)=>{const output=new java.util.ArrayList;return array.forEach(entry=>{output.add(entry)}),output},define:(object,consumer)=>{consumer||(consumer=entry=>({get:()=>entry.value,set:value=>{entry.value=value}}));const output={};return framework.entries(object).forEach(entry=>{const info=consumer(entry);info&&Object.defineProperty(output,entry.key,{get(){if(info.get)return info.get()},set(value){info.set&&info.set(value)}})}),output},entries:object=>framework.keys(object).map(key=>({key:key,value:object[key]})),extend:(object,...objects)=>Object.assign(object,...objects),flat:array=>array.filter(entry=>entry),interval:(script,period)=>{const state={iteration:null},loop=()=>{try{script()}catch(error){console.error(error)}state.iteration=framework.timeout(loop,period)};state.iteration=framework.timeout(loop,period);const output={cancel:()=>state.iteration.cancel()};return tasks.push(output),output},key:(object,value)=>framework.keys(object)[framework.values(object).indexOf(value)],keys:object=>Object.keys(object),lower:string=>string.toLowerCase(),match:(object,filter)=>{switch(typeof filter){case"undefined":return!0;case"function":return filter(object);case"object":switch(framework.type(filter)){case"Array":return filter.map(entry=>framework.match(object,entry)).includes(!0);case"Object":return!framework.keys(filter).map(key=>framework.match(object[key],filter[key])).includes(!1);default:return object===filter}default:return object===filter}},mirror:options=>{options||(options={});const mirror=framework.extend(options.array||[],{add:options.add||(()=>{}),remove:options.remove||(()=>{}),clear:options.clear||(()=>{})});return{get:()=>mirror,set:value=>{mirror.clear(),value.forEach(mirror.add)}}},object:(array,consumer)=>(consumer||(consumer=(entry,index)=>({[entry.key||index]:entry.value||entry})),framework.extend({},...framework.flat(array.map(consumer)))),pascal:(string,separator)=>string.split(separator).map(chunk=>framework.upper(chunk[0])+framework.lower(chunk.slice(1))).join(""),simplify:(...context)=>{let output=null;const object=context[0];switch(framework.type(object)){case"Array":output=[];for(let entry of object)context.includes(entry)||output.push(framework.simplify(entry,...context));output=framework.flat(output),output.length||(output=null);break;case"Object":output={};for(let key of framework.keys(object))context.includes(object[key])||(output[key]=framework.simplify(object[key],...context));output=framework.strain(output),framework.keys(output).length||(output=null);break;default:output=object}return output},splice:(string,separator,...index)=>string.split(separator).slice(...index).join(separator),strain:(object,consumer)=>(consumer||(consumer=entry=>entry.value),framework.object(framework.entries(object).filter(consumer))),timeout:(script,period)=>{const state={cancel:!1},runnable=new(Java.extend(Java.type("java.lang.Runnable")))({run:()=>state.cancel||script()});server.getScheduler().runTaskLater(core.plugin,runnable,Math.ceil(period/50));const output={cancel:()=>state.cancel=!0};return tasks.push(output),output},type:object=>{const type=toString.apply(object).split(" ")[1].slice(0,-1);return"Object"===type?object.constructor.name:type},upper:string=>string.toUpperCase(),uuid:string=>string?java.util.UUID.fromString(string):java.util.UUID.randomUUID(),values:object=>Object.values(object)};core.event("org.bukkit.event.server.PluginDisableEvent",event=>{event.getPlugin()===core.plugin&&tasks.forEach(task=>task.cancel())}),core.export(framework); \ No newline at end of file