Skip to content
This repository was archived by the owner on Jan 31, 2022. It is now read-only.

Commit a3ecebb

Browse files
committed
First commit
0 parents  commit a3ecebb

11 files changed

Lines changed: 361 additions & 0 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/.idea/
2+
FormAPI.iml
3+
/target/

pom.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>ru.nukkitx</groupId>
8+
<artifactId>FormAPI</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<dependencies>
12+
<dependency>
13+
<groupId>cn.nukkit</groupId>
14+
<artifactId>nukkit</artifactId>
15+
<version>1.0-SNAPSHOT</version>
16+
<scope>provided</scope>
17+
</dependency>
18+
</dependencies>
19+
</project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package ru.nukkitx;
2+
3+
import cn.nukkit.plugin.PluginBase;
4+
import ru.nukkitx.events.EventListener;
5+
6+
public class FormAPI extends PluginBase {
7+
@Override
8+
public void onEnable() {
9+
this.getServer().getPluginManager().registerEvents(new EventListener(), this);
10+
}
11+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package ru.nukkitx.events;
2+
3+
import cn.nukkit.Player;
4+
import cn.nukkit.event.EventHandler;
5+
import cn.nukkit.event.EventPriority;
6+
import cn.nukkit.event.Listener;
7+
import cn.nukkit.event.player.PlayerFormRespondedEvent;
8+
import cn.nukkit.event.player.PlayerQuitEvent;
9+
import cn.nukkit.form.response.FormResponse;
10+
import cn.nukkit.form.response.FormResponseCustom;
11+
import cn.nukkit.form.response.FormResponseModal;
12+
import cn.nukkit.form.response.FormResponseSimple;
13+
import cn.nukkit.form.window.FormWindow;
14+
import cn.nukkit.form.window.FormWindowCustom;
15+
import cn.nukkit.form.window.FormWindowModal;
16+
import cn.nukkit.form.window.FormWindowSimple;
17+
import ru.nukkitx.forms.Form;
18+
19+
import java.util.ArrayList;
20+
21+
public class EventListener implements Listener {
22+
23+
@EventHandler(priority = EventPriority.HIGH)
24+
public void formResponded(PlayerFormRespondedEvent event) {
25+
Player player = event.getPlayer();
26+
FormWindow window = event.getWindow();
27+
FormResponse response = window.getResponse();
28+
29+
if (response == null) return;
30+
31+
if (Form.playersForm.containsKey(player.getName())) {
32+
Object data;
33+
34+
if (event.wasClosed()) {
35+
data = null;
36+
Form.playersForm.get(player.getName()).handle(player, window, data, Form.paramsForm.get(player.getName()));
37+
return;
38+
}
39+
40+
if (window instanceof FormWindowSimple) {
41+
data = ((FormResponseSimple) response).getClickedButtonId();
42+
Form.playersForm.get(player.getName()).handle(player, window, data, Form.paramsForm.get(player.getName()));
43+
return;
44+
}
45+
46+
if (window instanceof FormWindowCustom) {
47+
data = new ArrayList(((FormResponseCustom) response).getResponses().values());
48+
Form.playersForm.get(player.getName()).handle(player, window, data, Form.paramsForm.get(player.getName()));
49+
return;
50+
}
51+
52+
if (window instanceof FormWindowModal) {
53+
data = ((FormResponseModal) response).getClickedButtonId();
54+
Form.playersForm.get(player.getName()).handle(player, window, data, Form.paramsForm.get(player.getName()));
55+
return;
56+
}
57+
}
58+
}
59+
60+
@EventHandler(priority = EventPriority.HIGH)
61+
public void playerQuit(PlayerQuitEvent event) {
62+
Player player = event.getPlayer();
63+
64+
Form.playersForm.remove(player.getName());
65+
Form.paramsForm.remove(player.getName());
66+
}
67+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package ru.nukkitx.forms;
2+
3+
import cn.nukkit.Player;
4+
import cn.nukkit.form.window.FormWindow;
5+
6+
import java.util.HashMap;
7+
8+
public abstract class Form {
9+
public static HashMap<String, FormResponse> playersForm = new HashMap();
10+
public static HashMap<String, HashMap<String, Object>> paramsForm = new HashMap();
11+
protected FormWindow form;
12+
13+
public final void send(Player player, FormResponse response) {
14+
playersForm.put(player.getName(), response);
15+
paramsForm.put(player.getName(), null);
16+
player.showFormWindow(form);
17+
}
18+
19+
public final void send(Player player, FormResponse response, HashMap<String, Object> params) {
20+
playersForm.put(player.getName(), response);
21+
paramsForm.put(player.getName(), params);
22+
player.showFormWindow(form);
23+
}
24+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package ru.nukkitx.forms;
2+
3+
import cn.nukkit.Player;
4+
import cn.nukkit.form.window.FormWindow;
5+
6+
import java.util.HashMap;
7+
8+
public interface FormResponse {
9+
10+
/*
11+
*
12+
* player - Игрок.
13+
* window - Форма, полученного результата.
14+
* data - Результат формы (является null, если форма была закрыта)
15+
*
16+
*/
17+
18+
void handle(Player player, FormWindow window, Object data, HashMap<String, Object> params);
19+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package ru.nukkitx.forms.elements;
2+
3+
import cn.nukkit.form.element.*;
4+
import cn.nukkit.form.window.FormWindowCustom;
5+
import ru.nukkitx.forms.Form;
6+
7+
import java.util.List;
8+
9+
public class CustomForm extends Form {
10+
11+
public CustomForm() {
12+
form = new FormWindowCustom("");
13+
}
14+
15+
public CustomForm(String title) {
16+
form = new FormWindowCustom(title);
17+
}
18+
19+
public CustomForm setTitle(String value) {
20+
((FormWindowCustom) form).setTitle(value);
21+
return this;
22+
}
23+
24+
public CustomForm addLabel(String value) {
25+
((FormWindowCustom) form).addElement(new ElementLabel(value));
26+
return this;
27+
}
28+
29+
public CustomForm addInput() {
30+
ElementInput element = new ElementInput("");
31+
((FormWindowCustom) form).addElement(element);
32+
return this;
33+
}
34+
35+
public CustomForm addInput(String name) {
36+
ElementInput element = new ElementInput(name);
37+
((FormWindowCustom) form).addElement(element);
38+
return this;
39+
}
40+
41+
public CustomForm addInput(String name, String placeholder) {
42+
ElementInput element = new ElementInput(name, placeholder);
43+
((FormWindowCustom) form).addElement(element);
44+
return this;
45+
}
46+
47+
public CustomForm addInput(String name, String placeholder, String defaultText) {
48+
ElementInput element = new ElementInput(name, placeholder, defaultText);
49+
((FormWindowCustom) form).addElement(element);
50+
return this;
51+
}
52+
53+
public CustomForm addToggle() {
54+
ElementToggle element = new ElementToggle("");
55+
((FormWindowCustom) form).addElement(element);
56+
return this;
57+
}
58+
59+
public CustomForm addToggle(String name) {
60+
ElementToggle element = new ElementToggle(name);
61+
((FormWindowCustom) form).addElement(element);
62+
return this;
63+
}
64+
65+
public CustomForm addToggle(String name, boolean defaultValue) {
66+
ElementToggle element = new ElementToggle(name, defaultValue);
67+
((FormWindowCustom) form).addElement(element);
68+
return this;
69+
}
70+
71+
public CustomForm addDropDown(String name, List<String> list) {
72+
ElementDropdown element = new ElementDropdown(name, list);
73+
((FormWindowCustom) form).addElement(element);
74+
75+
return this;
76+
}
77+
78+
public CustomForm addDropDown(String name, List<String> list, int defaultValue) {
79+
ElementDropdown element = new ElementDropdown(name, list, defaultValue);
80+
((FormWindowCustom) form).addElement(element);
81+
return this;
82+
}
83+
84+
public CustomForm addSlider(String name, int min, int max) {
85+
ElementSlider element = new ElementSlider(name, min, max);
86+
((FormWindowCustom) form).addElement(element);
87+
88+
return this;
89+
}
90+
91+
public CustomForm addSlider(String name, int min, int max, int step) {
92+
ElementSlider element = new ElementSlider(name, min, max, step, 3);
93+
((FormWindowCustom) form).addElement(element);
94+
return this;
95+
}
96+
97+
public CustomForm addSlider(String name, int min, int max, int step, int defaultValue) {
98+
ElementSlider element = new ElementSlider(name, min, max, step, defaultValue);
99+
((FormWindowCustom) form).addElement(element);
100+
return this;
101+
}
102+
103+
public CustomForm addStepSlider(String name, List<String> list) {
104+
ElementStepSlider element = new ElementStepSlider(name, list);
105+
((FormWindowCustom) form).addElement(element);
106+
return this;
107+
}
108+
109+
public CustomForm addStepSlider(String name, List<String> list, int defaultStep) {
110+
ElementStepSlider element = new ElementStepSlider(name, list, defaultStep);
111+
((FormWindowCustom) form).addElement(element);
112+
return this;
113+
}
114+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package ru.nukkitx.forms.elements;
2+
3+
public enum ImageType {
4+
PAHT, URL
5+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package ru.nukkitx.forms.elements;
2+
3+
import cn.nukkit.form.window.FormWindowModal;
4+
import ru.nukkitx.forms.Form;
5+
6+
public class ModalForm extends Form {
7+
8+
public ModalForm() {
9+
form = new FormWindowModal("", "", "", "");
10+
}
11+
12+
public ModalForm(String title) {
13+
form = new FormWindowModal(title, "", "", "");
14+
}
15+
16+
public ModalForm(String title, String content) {
17+
form = new FormWindowModal(title, content, "", "");
18+
}
19+
20+
public ModalForm(String title, String content, String trueButton) {
21+
form = new FormWindowModal(title, content, trueButton, "");
22+
}
23+
24+
public ModalForm(String title, String content, String trueButton, String falseButton) {
25+
form = new FormWindowModal(title, content, trueButton, falseButton);
26+
}
27+
28+
public ModalForm setTitle(String value) {
29+
((FormWindowModal) form).setTitle(value);
30+
31+
return this;
32+
}
33+
34+
public ModalForm setContent(String value) {
35+
((FormWindowModal) form).setContent(value);
36+
return this;
37+
}
38+
39+
public ModalForm setButton1(String value) {
40+
((FormWindowModal) form).setButton1(value);
41+
return this;
42+
}
43+
44+
public ModalForm setButton2(String value) {
45+
((FormWindowModal) form).setButton2(value);
46+
return this;
47+
}
48+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package ru.nukkitx.forms.elements;
2+
3+
import cn.nukkit.form.element.ElementButton;
4+
import cn.nukkit.form.element.ElementButtonImageData;
5+
import cn.nukkit.form.window.FormWindowSimple;
6+
import ru.nukkitx.forms.Form;
7+
8+
public class SimpleForm extends Form {
9+
10+
public SimpleForm() {
11+
form = new FormWindowSimple("", "");
12+
}
13+
14+
public SimpleForm(String title) {
15+
form = new FormWindowSimple(title, "");
16+
}
17+
18+
public SimpleForm(String title, String content) {
19+
form = new FormWindowSimple(title, content);
20+
}
21+
22+
public SimpleForm setTitle(String value) {
23+
((FormWindowSimple) form).setTitle(value);
24+
return this;
25+
}
26+
27+
public SimpleForm setContent(String value) {
28+
((FormWindowSimple) form).setContent(value);
29+
return this;
30+
}
31+
32+
public SimpleForm addButton(String text) {
33+
((FormWindowSimple) form).addButton(new ElementButton(text));
34+
return this;
35+
}
36+
37+
public SimpleForm addButton(String text, ImageType type, String ico) {
38+
ElementButton button = new ElementButton(text);
39+
button.addImage(new ElementButtonImageData((type == ImageType.PAHT) ? "path" : "url", ico));
40+
((FormWindowSimple) form).addButton(button);
41+
return this;
42+
}
43+
}

0 commit comments

Comments
 (0)