-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Create README.md * Update README.md * Add files via upload * Update index.js * Update index.js * Update test.js * Update index.js * pass format checks * rename ProtoForm to protoform --------- Co-authored-by: Jayly <65847850+JaylyDev@users.noreply.github.com>
- Loading branch information
Showing
6 changed files
with
256 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<void>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
// Script example for ScriptAPI | ||
// Author: GamerFile <https://github.com/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)); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
}) |
This file was deleted.
Oops, something went wrong.