diff --git a/scripts/minecraft-language/index.js b/scripts/minecraft-language/index.js index a638829b..f7f14218 100644 --- a/scripts/minecraft-language/index.js +++ b/scripts/minecraft-language/index.js @@ -1,3 +1,6 @@ +// Script example for ScriptAPI +// Author: bot174 +// Project: https://github.com/JaylyDev/ScriptAPI export const languageKeys = [ "accessibility.disableTTS", "accessibility.enableTTS", diff --git a/scripts/protoform/README.md b/scripts/protoform/README.md new file mode 100644 index 00000000..79b6b1c6 --- /dev/null +++ b/scripts/protoform/README.md @@ -0,0 +1,80 @@ +# ProtoForm + +**ProtoForm** is a versatile library for creating and managing various types of forms in your Minecraft server plugins. It simplifies the process of generating modal, message, and action forms, providing a clean and structured way to handle user input. + +## Features + +- **Modal Forms:** Easily create modal forms with different types of fields such as text, slider, dropdown, and toggle. + +- **Message Forms:** Construct message forms with customizable buttons, allowing for user interaction. + +- **Action Forms:** Create action forms with multiple buttons, each with its own text and optional texture. + +## Installation + +This could be easily installed through **NPM** + +## Usage + +### Import ProtoForm + +```js +import { ProtoForm } from "./index.js"; +``` +### ModalForm + +```js +const modalForm = new ProtoForm({ + title: "Example Modal Form", + fields: [ + ["text", ["Label", "Placeholder", "Default"]], + ["slider",["Label",2/* Min */,10/* Max */,2/*Step*/,6/*Default*/]], + ["dropdown",["Label",["Option 1","Option 2", "Option 3"]/* Options */,1 /*Default*/]], + ["toggle",["Label",true /*default*/]] + ], + response: (data) => { + // Handle form submission + console.warn("Modal Form submitted:" + data.formValues.join(" ")); + } +}); +``` +### MessageForm + +```js +const messageForm = new ProtoForm({ + title: "Example Message Form", + body: "This is a message form example.", + btn1: "OK", + btn2: "Cancel", + response: (data) => { + // Handle form submission + console.warn("Message Form submitted: " + data.selection); + } +}); +``` +### ActionForm + +```js +const actionForm = new ProtoForm({ + title: "Example Action Form", + btns: [ + ["Button 1", "path/to/btn1_texture"], + ["Button 2", "path/to/btn2_texture"], + // Add more buttons as needed + ], + response: (data) => { + // Handle form submission + console.warn("Action Form submitted:" + data.selection); + } +}); +``` +### Showing Forms To Player + +```js +// Assuming 'player' is an instance of '@minecraft/server' Player +modalForm.show(player); +messageForm.show(player); +actionForm.show(player); +``` +## Author +These Scripts Are Written By **GamerFile** diff --git a/scripts/protoform/index.d.ts b/scripts/protoform/index.d.ts new file mode 100644 index 00000000..bc13cb65 --- /dev/null +++ b/scripts/protoform/index.d.ts @@ -0,0 +1,22 @@ +import { Player } from "@minecraft/server"; +import { MessageFormData, ActionFormData, ModalFormData } from "@minecraft/server-ui"; +interface FormResponse { + [key: string]: any; +} + +declare class ProtoForm { + constructor(options: { + title: string; + fields?: [string, any[]][]; + body?: string; + btn1?: string; + btn2?: string; + btns?: [string, string | undefined][]; + response?: (result: FormResponse) => void; + }); + + form: MessageFormData | ActionFormData | ModalFormData | null; + response: (result: FormResponse) => void; + + show(player: Player): Promise; +} diff --git a/scripts/protoform/index.js b/scripts/protoform/index.js new file mode 100644 index 00000000..d526786a --- /dev/null +++ b/scripts/protoform/index.js @@ -0,0 +1,134 @@ +// Script example for ScriptAPI +// Author: GamerFile +// Project: https://github.com/JaylyDev/ScriptAPI +import { ActionFormData, ModalFormData, MessageFormData} from "@minecraft/server-ui"; + +/** + * Represents a form object. + */ +export class ProtoForm { + /** + * Initializes a new instance of the ProtoForm class. + * @param {Object} options The options for the form. + * @param {string} options.title The title of the form. + * @param {Object[]} options.fields An array of objects representing form elements for the modal form. + * @param {string} options.body The body text of the form. + * @param {string} options.btn1 The text of the first button for the message form. + * @param {string} options.btn2 The text of the second button for the message form. + * @param {string[]} options.btns An array of button texts for the action form. + * @param {Function} options.response The function to execute when the form is submitted. + */ + constructor({ + title, + fields, + body, + btn1, + btn2, + btns, + response + }) { + /** + * The form object. + * @type {any} + */ + this.form = null; + + /** + * The function to execute when the form is submitted. + * @type {Function} + */ + this.response = response ? response : () => {}; + + if (!title) { + throw new Error("Form Can't Be Created Without Title"); + } + + if (btn1 || btn2) { + if (fields || btns) { + throw new Error("Cannot assign 'fields' or 'btns' to type Message"); + } + + this.form = new MessageFormData(); + if (this.form instanceof MessageFormData) { + if (!btn1 || !btn2 || !body) { + throw new Error("One Or Two Of The Essential Properties Of MessageFormData Is Missing. Maybe it's btn1 Or btn2 Or body. Form Can't Be Created In Such A Condition."); + } + this.form.title(title); + this.form.body(body); + this.form?.button1(btn1); + this.form?.button2(btn2); + } + } else if (btns) { + if (fields || btn1 || btn2) { + throw new Error("Cannot assign 'fields' or 'btn1' or 'btn2' to type Action"); + } + + this.form = new ActionFormData(); + if (this.form instanceof ActionFormData) { + if (!body) throw new Error("Body is essential in ActionFormData."); + this.form.title(title); + this.form.body(body); + btns.forEach(([text, texture]) => { + this.form?.button( + text, + texture ?? undefined + ); + }); + } + } else if (fields) { + if (btns || btn1 || btn2) { + throw new Error("Cannot Use btns or btn1 or btn2 property in type Modal"); + } + this.form = new ModalFormData(); + if (this.form instanceof ModalFormData) { + this.form.title(title); + + fields.forEach(([type, details]) => { + switch (type) { + case "text": + this.form?.textField( + details[0], // label + details[1] || "", // placeholder + details[2] || "" // default + ); + break; + case "slider": + this.form?.slider( + details[0], // label + details[1], // min + details[2], // max + details[3] || 1, // step + details[4] || details[1] // default + ); + break; + case "dropdown": + this.form?.dropdown( + details[0], // label + details[1], // options + details[2] || 0 // default + ); + break; + case "toggle": + this.form?.toggle( + details[0], // label + details[1] || false // default + ); + break; + default: + throw new Error(`Invalid form element type: ${type}`); + } + }); + } + } + } + + /** + * Shows the form to the player and returns a Promise that resolves when the form is submitted. + * @param {import("@minecraft/server").Player} player The player to show the form to. + * @returns {Promise} A Promise that resolves when the form is submitted. + */ + show(player) { + return this.form?.show(player).then((response) => this.response(response)); + } +} + diff --git a/scripts/protoform/tests.js b/scripts/protoform/tests.js new file mode 100644 index 00000000..3e9872f5 --- /dev/null +++ b/scripts/protoform/tests.js @@ -0,0 +1,17 @@ +import { + world,Player +} from "@minecraft/server"; +import { ProtoForm } from "index.js"; +const actionform = new ProtoForm({ + title: "Test ActionForm", + body: "Body...", + btns: [["Hey","Texture Path"],["Btn2","Path2"]], + response: ({selection:s}) => { + console.warn("selected no" + s) + } +}) +world.afterEvents.entityHitBlock.subscribe(({damagingEntity: player}) => { + if (player instanceof Player) { + actionform.show(player) + } +}) diff --git a/scripts/structures/README.md b/scripts/structures/README.md deleted file mode 100644 index d6de167d..00000000 --- a/scripts/structures/README.md +++ /dev/null @@ -1,7 +0,0 @@ -# structures - -## Description -Includes vanilla structures path and vanilla structure size. Mirror from [DarkGamerYT/Bedrock-Editor-Extension](https://github.com/DarkGamerYT/Bedrock-Editor-Extension). - -## Credits -These scripts were written by [xKingDark](https://github.com/DarkGamerYT), [JaylyDev](https://github.com/JaylyDev), [Mojang](https://github.com/Mojang), [SmokeyStack](https://github.com/SmokeyStack)