Skip to content

Update In protoform #345

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 16 commits into from
Jan 4, 2024
34 changes: 29 additions & 5 deletions scripts/protoform/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 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.
**ProtoForm** simplifies the creation and management of Minecraft forms and responses. With a single class, it provides an efficient and easy way to generate various forms. This can save you time compared to the original approach, as minimal code is required. The form type is determined based on the keys/properties provided in the object, resulting in clean and concise code with reduced storage needs.

## Features

Expand All @@ -23,14 +23,16 @@ import { ProtoForm } from "./index.js";
```
### ModalForm

- **fields** key determines that it's a **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*/]]
["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
Expand All @@ -40,6 +42,8 @@ const modalForm = new ProtoForm({
```
### MessageForm

- **btn1** and **btn2** determines that it's a **MessageForm**

```js
const messageForm = new ProtoForm({
title: "Example Message Form",
Expand All @@ -54,12 +58,17 @@ const messageForm = new ProtoForm({
```
### ActionForm

- **btns** key determine that it's an **ActionForm**

```js
const actionForm = new ProtoForm({
title: "Example Action Form",
body: "Example Action Form Body",
btns: [
["Button 1", "path/to/btn1_texture"],
["Button 2", "path/to/btn2_texture"],
"Button 3",
["Button 4"],
// Add more buttons as needed
],
response: (data) => {
Expand All @@ -68,6 +77,7 @@ const actionForm = new ProtoForm({
}
});
```

### Showing Forms To Player

```js
Expand All @@ -76,5 +86,19 @@ modalForm.show(player);
messageForm.show(player);
actionForm.show(player);
```

### Case Errors

- When **fields** and **btn1** or **btn2** keys are used togethor, it throws error.

- When **fields** and **btns** keys are used togethor, it throws error.

- When **btns** and **btn1** or **btn2** keys are used togethor, it throws error.

- When **title** is missing, it throws error.

- When **body** is missing with **btns** or **btn1** and **btn2**, it throws error. => When **body** is missing in **MessageForm** or **ActionForm**, it throws error.


## Author
These Scripts Are Written By **GamerFile**
2 changes: 1 addition & 1 deletion scripts/protoform/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ declare class ProtoForm {
body?: string;
btn1?: string;
btn2?: string;
btns?: [string, string | undefined][];
btns?: (string | [string] | [string, string | undefined])[];
response?: (result: FormResponse) => void;
});

Expand Down
9 changes: 3 additions & 6 deletions scripts/protoform/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,8 @@ export class ProtoForm {
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
);
btns.forEach((btn) => {
typeof btn == "string"? this.form?.button(btn): this.form?.button(btn[0],btn[1] ?? undefined)
});
}
} else if (fields) {
Expand All @@ -83,7 +80,7 @@ export class ProtoForm {
if (this.form instanceof ModalFormData) {
this.form.title(title);

fields.forEach(([type, details]) => {
fields.forEach(([type, ...details]) => {
switch (type) {
case "text":
this.form?.textField(
Expand Down
2 changes: 1 addition & 1 deletion scripts/protoform/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ProtoForm } from "index.js";
const actionform = new ProtoForm({
title: "Test ActionForm",
body: "Body...",
btns: [["Hey","Texture Path"],["Btn2","Path2"]],
btns: [["Hey","Texture Path"],["Btn2","Path2"],"btn3","btn4",["btn5"]],
response: ({selection:s}) => {
console.warn("selected no" + s)
}
Expand Down