|
| 1 | +package de.mariocst.deviceban.formutils.custom; |
| 2 | + |
| 3 | +import cn.nukkit.Player; |
| 4 | +import cn.nukkit.form.element.Element; |
| 5 | +import cn.nukkit.form.response.FormResponseCustom; |
| 6 | +import cn.nukkit.form.window.FormWindowCustom; |
| 7 | +import de.mariocst.deviceban.DeviceBan; |
| 8 | +import de.mariocst.deviceban.formutils.FormHandler; |
| 9 | + |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.function.BiConsumer; |
| 12 | +import java.util.function.Consumer; |
| 13 | + |
| 14 | +public class CustomForm { |
| 15 | + private final DeviceBan plugin; |
| 16 | + |
| 17 | + private final String title; |
| 18 | + private final ArrayList<Element> elements; |
| 19 | + private final Consumer<Player> closeCallback; |
| 20 | + private final BiConsumer<Player, FormResponseCustom> submitCallback; |
| 21 | + |
| 22 | + public CustomForm(DeviceBan plugin, Builder b) { |
| 23 | + this.plugin = plugin; |
| 24 | + this.title = b.title; |
| 25 | + this.elements = b.elements; |
| 26 | + this.closeCallback = b.closeCallback; |
| 27 | + this.submitCallback = b.submitCallback; |
| 28 | + } |
| 29 | + |
| 30 | + public void send(Player player) { |
| 31 | + FormWindowCustom form = new FormWindowCustom(title); |
| 32 | + elements.forEach(form::addElement); |
| 33 | + |
| 34 | + FormHandler.customPending.put(player.getName(), this); |
| 35 | + player.showFormWindow(form); |
| 36 | + |
| 37 | + this.plugin.getServer().getScheduler().scheduleDelayedTask(this.plugin, () -> player.sendExperience(player.getExperience()), 20); |
| 38 | + } |
| 39 | + |
| 40 | + public void setClosed(Player player) { |
| 41 | + if (closeCallback == null) return; |
| 42 | + |
| 43 | + closeCallback.accept(player); |
| 44 | + } |
| 45 | + |
| 46 | + public void setSubmitted(Player player, FormResponseCustom form) { |
| 47 | + submitCallback.accept(player, form); |
| 48 | + } |
| 49 | + |
| 50 | + public static class Builder { |
| 51 | + private final DeviceBan plugin; |
| 52 | + |
| 53 | + private final String title; |
| 54 | + private final ArrayList<Element> elements = new ArrayList<>(); |
| 55 | + private Consumer<Player> closeCallback; |
| 56 | + private BiConsumer<Player, FormResponseCustom> submitCallback; |
| 57 | + |
| 58 | + public Builder(DeviceBan plugin, String title) { |
| 59 | + this.plugin = plugin; |
| 60 | + this.title = title; |
| 61 | + } |
| 62 | + |
| 63 | + public Builder addElement(Element element) { |
| 64 | + elements.add(element); |
| 65 | + return this; |
| 66 | + } |
| 67 | + |
| 68 | + public Builder onClose(Consumer<Player> cb) { |
| 69 | + this.closeCallback = cb; |
| 70 | + return this; |
| 71 | + } |
| 72 | + |
| 73 | + public Builder onSubmit(BiConsumer<Player, FormResponseCustom> cb) { |
| 74 | + this.submitCallback = cb; |
| 75 | + return this; |
| 76 | + } |
| 77 | + |
| 78 | + public CustomForm build() { |
| 79 | + return new CustomForm(this.plugin, this); |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments