├── .gitignore ├── .poggit.yml ├── LICENSE ├── README.md ├── plugin.yml └── src └── dktapps └── pmforms_demo └── Main.php /.gitignore: -------------------------------------------------------------------------------- 1 | /src/dktapps/pmforms/ 2 | .idea/ 3 | -------------------------------------------------------------------------------- /.poggit.yml: -------------------------------------------------------------------------------- 1 | --- # Poggit-CI Manifest. Open the CI at https://poggit.pmmp.io/ci/dktapps-pm-pl/pmforms-demo 2 | build-by-default: true 3 | branches: 4 | - master 5 | projects: 6 | pmforms-demo: 7 | path: "" 8 | libs: 9 | - src: dktapps-pm-pl/pmforms/pmforms 10 | version: ^2.0.0 11 | ... 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # pmforms-demo 2 | 3 | This plugin provides example code that shows how [pmforms](https://github.com/dktapps-pm-pl/pmforms) is expected to be used. 4 | 5 | It also supports a command `/form` that can be used to send sample forms to players to play with. 6 | 7 | ## Commands 8 | - `/form modal [players]` 9 | - `/form menu [players]` 10 | - `/form custom [players]` 11 | 12 | ## License 13 | Unlicense. 14 | -------------------------------------------------------------------------------- /plugin.yml: -------------------------------------------------------------------------------- 1 | name: pmforms-demo 2 | main: dktapps\pmforms_demo\Main 3 | version: 0.0.1 4 | api: [4.0.0, 5.0.0] 5 | load: POSTWORLD 6 | author: dktapps 7 | description: Demo plugin that shows off in-game forms, and how to use its API via code 8 | website: https://github.com/dktapps-pm-pl/pmforms-demo 9 | commands: 10 | form: 11 | description: "Allows sending an example form to the target player" 12 | usage: "/form [players ...]" 13 | permission: pmforms.command 14 | permissions: 15 | pmforms.command: 16 | default: true 17 | description: "Allows the use of /form" 18 | -------------------------------------------------------------------------------- /src/dktapps/pmforms_demo/Main.php: -------------------------------------------------------------------------------- 1 | getName() === "form" and count($args) >= 1){ 31 | $form = null; 32 | switch($args[0]){ 33 | case "modal": 34 | $form = $this->createModalForm(); 35 | break; 36 | case "menu": 37 | $form = $this->createMenuForm(); 38 | break; 39 | case "custom": 40 | $form = $this->createCustomForm(); 41 | break; 42 | default: 43 | return false; 44 | } 45 | 46 | $players = []; 47 | for($argIdx = 1; isset($args[$argIdx]); ++$argIdx){ 48 | $player = $this->getServer()->getPlayerByPrefix($args[$argIdx]); 49 | if($player === null){ 50 | $sender->sendMessage(TextFormat::RED . "Can't find a player by partial name " . $args[$argIdx]); 51 | return true; 52 | } 53 | $players[] = $player; 54 | } 55 | if(count($players) === 0){ 56 | if(!($sender instanceof Player)){ 57 | $sender->sendMessage(TextFormat::RED . "Please provide some players to send the form to!"); 58 | return true; 59 | } 60 | $players[] = $sender; 61 | } 62 | foreach($players as $player){ 63 | $player->sendForm($form); 64 | } 65 | return true; 66 | } 67 | return false; 68 | } 69 | 70 | private function createModalForm() : ModalForm{ 71 | return new ModalForm( 72 | "Example Modal Form", /* title of the form */ 73 | "Do you want to do something?", /* form body text */ 74 | 75 | /** 76 | * Called when any player who received this form submits a response. The player is passed to the 77 | * callback,so you can send the same form to multiple players at the same time without any issues, as 78 | * long as you don't tie the player to the form. 79 | * 80 | * @param Player $submitter the player who submitted this form 81 | * @param bool $choice the button clicked 82 | */ 83 | function(Player $submitter, bool $choice) : void{ 84 | /* callback to execute when the player submits the form */ 85 | $this->getServer()->broadcastMessage($submitter->getName() . " chose " . ($choice ? "YES" : "NO")); 86 | }, 87 | "YES", /* text to show on the "Yes" button, defaults to client-translated "Yes" string, this is optional */ 88 | "NO" /* text to show on the "No" button, defaults to client-translated "No" string, this is optional */ 89 | ); 90 | } 91 | 92 | private function createMenuForm() : MenuForm{ 93 | return new MenuForm( 94 | "Example Menu", /* title of the form */ 95 | "Please choose an option", /* body text, shown above the menu options */ 96 | [ 97 | /* menu option with no icon */ 98 | new MenuOption("Option 1"), 99 | 100 | /* menu option with PATH icon - PATH types have to be relative to the root of a resource pack */ 101 | new MenuOption("Option 2", new FormIcon("textures/blocks/acacia_trapdoor.png", FormIcon::IMAGE_TYPE_PATH)), 102 | 103 | /* menu option with URL icon - this should point to a valid online image URL */ 104 | new MenuOption("Option 3", new FormIcon("https://pbs.twimg.com/profile_images/776551947595833345/Og1CSz_c_400x400.jpg", FormIcon::IMAGE_TYPE_URL)) 105 | ], 106 | 107 | /** 108 | * Called when the player submits the form. 109 | * 110 | * @param Player $submitter 111 | * @param int $selected array offset of selected element: 0 => option 1, 1 => option 2, etc 112 | */ 113 | function(Player $submitter, int $selected) : void{ 114 | $this->getServer()->broadcastMessage(TextFormat::LIGHT_PURPLE . $submitter->getName() . " chose option " . ($selected + 1) . " from the menu!"); 115 | }, 116 | /** 117 | * Called when the player closes the form without selecting an option. This parameter is optional and 118 | * can be left blank. 119 | * 120 | * @param Player $submitter 121 | */ 122 | function(Player $submitter) : void{ 123 | $this->getServer()->broadcastMessage(TextFormat::RED . $submitter->getName() . " closed the menu :("); 124 | } 125 | ); 126 | } 127 | 128 | private function createCustomForm() : CustomForm{ 129 | return new CustomForm( 130 | "Example Custom Form", /* title of the form */ 131 | [ 132 | /* each element requires a string identifer that you'll use to fetch its value from the $response */ 133 | new Label("this_is_a_label", "You can add a range of different types of elements to this type of form. This one is a label."), 134 | new Dropdown("dropdown_identifier", "This is a dropdown", [ 135 | "hello", 136 | "world", 137 | "please", 138 | "choose", 139 | "an", 140 | "option" 141 | ]), 142 | new Input("a_text_field", "Please don't type your password in here", "This will show as faint text in the box"), 143 | new Input("a_text_field_2", "Please don't type your password in here either", "", "This appears in the box and the user can edit it"), 144 | new Slider("my_slider", "Slide to choose a value", 1.0, 10.0, 0.01, 5.0 /* selector will show 5 when sent, default is minimum */), 145 | new StepSlider("a_step_slider", "Slide to choose a preset option", [ 146 | "thing 1", 147 | "thing 2", 148 | "thing 3" 149 | ], 1 /* this makes the default point to "thing 2" instead of "thing 1" */), 150 | new Toggle("switch", "I'm a toggle!", true /* default value is false by default, this parameter is optional */) 151 | ], 152 | function(Player $submitter, CustomFormResponse $response) : void{ 153 | $this->getServer()->broadcastMessage(TextFormat::GREEN . $submitter->getName() . " submitted custom form with values: " . print_r($response, true)); 154 | }, 155 | function(Player $submitter) : void{ 156 | $this->getServer()->broadcastMessage(TextFormat::YELLOW . $submitter->getName() . " closed the form :("); 157 | } 158 | ); 159 | } 160 | } 161 | --------------------------------------------------------------------------------