diff --git a/plugin.yml b/plugin.yml index 80e571c..b22f2e4 100644 --- a/plugin.yml +++ b/plugin.yml @@ -1,14 +1,15 @@ --- name: CapesLocker -version: 1.2.0 -main: Verre2OuiSki\CapesLocker\CapesLocker +version: 2.0.0 +main: unarray\capeslocker\capeslocker +src-namespace-prefix: unarray\capeslocker api: 4.0.0 extensions: [gd] -author: Verre2OuiSki +author: Unarray description: Add your own capes to the server ! -website: https://github.com/Verre2OuiSki +website: https://github.com/Unarray # - - - - - - - - - - - - - - - - diff --git a/src/CapesLocker.php b/src/CapesLocker.php new file mode 100644 index 0000000..787d66a --- /dev/null +++ b/src/CapesLocker.php @@ -0,0 +1,20 @@ +getResources() as $file){ - $this->saveResource($file->getFilename()); - } - - $this->capes = (new Config( $this->getDataFolder() . "capes.json", Config::JSON ))->getAll(); - - $perm_manager = PermissionManager::getInstance(); - foreach( $this->capes as $cape_id => $cape ){ - if($cape["default"]){ - $this->default_capes[$cape_id] = $cape; - }else{ - $permission = new Permission( - "capeslocker.cape." . $cape_id, - "Allow players to use \" " . $cape["name"] . " \" cape" - ); - $perm_manager->addPermission($permission); - $perm_manager->getPermission(DefaultPermissions::ROOT_OPERATOR)->addChild($permission->getName(), true); - } - } - - $this->players_capes = new Config( $this->getDataFolder() . "players_capes.yml", Config::YAML ); - $this->wearing_cape = new Config( $this->getDataFolder() . "wearing_cape.yml", Config::YAML ); - - $this->getServer()->getPluginManager()->registerEvents( new SetCape($this), $this); - - $this->getServer()->getCommandMap()->register( $this->getName(), new Capes($this) ); - $this->getServer()->getCommandMap()->register( $this->getName(), new ManageCapes($this) ); - $this->getServer()->getCommandMap()->register( $this->getName(), new PlayersCapesCleaner($this) ); - } - - private function capeIdToCapeData( string $cape_id ){ - - $cape_file = $this->capes[$cape_id]["cape"]; - $path = $this->getDataFolder() . $cape_file . ".png"; - - $image = @imagecreatefrompng($path); - if ($image === false) { - throw new Exception("Couldn't load image"); - } - - $size = @imagesx($image) * @imagesy($image) * 4; - if ($size !== 64 * 32 * 4) { - throw new Exception("Invalid cape size"); - } - - $cape_data = ""; - for ($y = 0, $height = imagesy($image); $y < $height; $y++) { - for ($x = 0, $width = imagesx($image); $x < $width; $x++) { - $color = imagecolorat($image, $x, $y); - $cape_data .= pack("c", ($color >> 16) & 0xFF) //red - . pack("c", ($color >> 8) & 0xFF) //green - . pack("c", $color & 0xFF) //blue - . pack("c", 255 - (($color & 0x7F000000) >> 23)); //alpha - } - } - - imagedestroy($image); - return $cape_data; - } - - - -// - - - PLUGIN API - - /** - * Return all capes - * @return array - */ - public function getCapes(){ - return $this->capes; - } - - /** - * Return all default capes - * @return array - */ - public function getDefaultCapes(){ - return $this->default_capes; - } - - /** - * Return cape info - * @param string $cape_id - * @return NULL|array - */ - public function getCapeById($cape_id){ - return $this->capes[$cape_id] ?? NULL; - } - - /** - * Return unlocked player's capes (default capes isn't include) - * @param Player $player Player to get his capes - * @return array - */ - public function getPlayerCapes($player){ - $this->players_capes->reload(); - $player_capes_id = $this->players_capes->get( - $player->getUniqueId()->toString(), - [] - ); - - $player_capes = []; - foreach ($player_capes_id as $cape_id) { - $cape = $this->getCapeById($cape_id); - if($cape){ - $player_capes[$cape_id] = $cape; - } - } - return $player_capes; - } - - /** - * Return cape player wearing - * @param Player $player - * @return null|string - */ - public function getWearingCapeId( Player $player ) : null|string{ - $this->wearing_cape->reload(); - return $this->wearing_cape->get($player->getUniqueId()->toString(), null); - } - - /** - * Return all wearing capes - * @return Config - */ - public function getWearingCapes() : Config{ - return $this->wearing_cape; - } - - /** - * Return permitted player's capes (default capes isn't include) - * @param Player $player Player to get his capes - * @return array - */ - public function getPlayerPermittedCapes($player){ - - $player_capes = []; - $capes = array_diff_key( $this->capes, $this->default_capes ); - - foreach($capes as $cape_id => $cape){ - if( $player->hasPermission( "capeslocker.cape." . $cape_id ) ){ - $player_capes[$cape_id] = $cape; - } - } - - return $player_capes; - } - - /** - * Get alls players capes (default capes and permitted capes isn't include) - * @return Config - */ - public function getPlayersCapes(){ - return $this->players_capes; - } - - /** - * Unlock a cape for a specific player - * @param Player $player Player to unlock a cape - * @param string $cape_id The ID of the cape to unlock - * @return void - */ - public function unlockCape( $player, $cape_id ){ - - if($this->hasCape($player, $cape_id)) return; - - $player_uuid = $player->getUniqueId()->toString(); - - $this->players_capes->reload(); - - if($this->players_capes->exists($player_uuid)){ - - $player_capes = $this->players_capes->get($player_uuid); - array_push($player_capes, $cape_id); - $this->players_capes->set( $player_uuid, $player_capes ); - - }else{ - $this->players_capes->set( $player_uuid, [$cape_id] ); - } - - $this->players_capes->save(); - } - - /** - * lock a cape for a specific player - * @param Player $player Player to lock a cape - * @param string $cape_id The ID of the cape to lock - * @return void - */ - public function lockCape( $player, $cape_id){ - - // if cape is unlock by default OR player doesn't have this cape - if($this->capes[$cape_id]["default"] || !$this->hasCape($player, $cape_id)) return; - - $player_uuid = $player->getUniqueId()->toString(); - - $this->players_capes->reload(); - $player_capes = $this->players_capes->get($player_uuid); - - $cape_id_index = array_search($cape_id, $player_capes); - unset($player_capes[$cape_id_index]); - - if(empty($player_capes)){ - $this->players_capes->remove($player_uuid); - }else{ - $this->players_capes->set( $player_uuid, $player_capes); - } - $this->players_capes->save(); - } - - /** - * Unlock a cape for a specific player - * @param Player $player Player to equip the cape with - * @param string $cape_id The ID of the cape to be equipped - * @return void - */ - public function setPlayerCape( $player, $cape_id = null ){ - - $old_skin = $player->getSkin(); - $this->wearing_cape->reload(); - - if(is_null($cape_id)){ - - $wear_cape = $this->wearing_cape->get($player->getUniqueId()->toString()); - - if($wear_cape){ - $this->wearing_cape->remove($player->getUniqueId()->toString()); - $this->wearing_cape->save(); - } - - $player->setSkin( - new Skin( - $old_skin->getSkinId(), - $old_skin->getSkinData(), - "", - $old_skin->getGeometryName(), - $old_skin->getGeometryData() - ) - ); - $player->sendSkin(); - return; - } - - - $this->wearing_cape->set($player->getUniqueId()->toString(), $cape_id); - $this->wearing_cape->save(); - - $cape_data = $this->capeIdToCapeData($cape_id); - - $new_skin = new Skin( - $old_skin->getSkinId(), - $old_skin->getSkinData(), - $cape_data, - $old_skin->getGeometryName(), - $old_skin->getGeometryData() - ); - - $player->setSkin($new_skin); - $player->sendSkin(); - } - - /** - * Check if a player have a specific cape - * @param Player $player Player to check their capes - * @param string $cape_id The ID of the cape to check - * @return bool - */ - public function hasCape( $player, $cape_id ){ - - if( $this->capes[$cape_id]["default"] ) return true; - if( $player->hasPermission( "capeslocker.cape." . $cape_id ) ) return true; - - $this->players_capes->reload(); - $player_capes = $this->players_capes->get( - $player->getUniqueId()->toString() - ); - return $player_capes ? in_array($cape_id, $player_capes) : false; - } - -} \ No newline at end of file diff --git a/src/Verre2OuiSki/CapesLocker/Commands/Capes.php b/src/Verre2OuiSki/CapesLocker/Commands/Capes.php deleted file mode 100644 index 43f727b..0000000 --- a/src/Verre2OuiSki/CapesLocker/Commands/Capes.php +++ /dev/null @@ -1,188 +0,0 @@ -plugin = $plugin; - - parent::__construct( - "capes", - "Open your capes locker menu !", - "/capes" - ); - $this->setPermission("capeslocker.command.capes"); - - $config = $this->plugin->getConfig(); - $this->cooldown = $config->get("cape_cooldown"); - $this->cooldown_message = str_replace( - "{cooldown}", - $this->cooldown, - $config->get("cooldown_message") - ); - $this->locked_cape_message = $config->get("locked_cape_message"); - $this->cape_equiped_message = $config->get("cape_equiped_message"); - $this->menu_title = $config->get("menu_title"); - $this->menu_body = $config->get("menu_body"); - } - - public function execute(CommandSender $sender, string $commandLabel, array $args){ - - // Sender doesn't have permission to execute this command - if(!$this->testPermission($sender, $this->getPermission())) return; - - - // Sender isn't a player - if(!$sender instanceof Player){ - $sender->sendMessage("§cYou must be a player to execute this command"); return; - } - - $sender->sendForm( $this->capesList($sender) ); - } - - - -// --- FORMS - private function capesList(Player $player){ - - $options = [ - new MenuOption("§4Remove", new FormIcon("textures/ui/realms_red_x", FormIcon::IMAGE_TYPE_PATH)) - ]; - $options_cape_link = []; - - $unlocked_capes = array_merge($this->plugin->getDefaultCapes(), $this->plugin->getPlayerPermittedCapes($player), $this->plugin->getPlayerCapes($player)); - $locked_capes = array_diff_key($this->plugin->getCapes(), $unlocked_capes); - - // Set player capes at top of the menu - foreach ($unlocked_capes as $cape_id => $cape) { - array_push( - $options, - new MenuOption( - $cape["name"], - new FormIcon("textures/ui/icon_unlocked", FormIcon::IMAGE_TYPE_PATH) - ) - ); - $options_cape_link[array_key_last($options)] = $cape_id; - - } - - // Locked capes after unlocked capes - foreach($locked_capes as $cape_id => $cape){ - - array_push( - $options, - new MenuOption( - $cape["name"], - new FormIcon("textures/ui/icon_lock", FormIcon::IMAGE_TYPE_PATH) - ) - ); - $options_cape_link[array_key_last($options)] = $cape_id; - } - - return new MenuForm( - $this->menu_title, - $this->menu_body, - $options, - function( Player $submitter, int $selected ) use ($options_cape_link) : void { - - if($selected == 0){ - - $this->plugin->setPlayerCape($submitter); - return; - } - - $submitter->sendForm( - $this->capeOptions( - $submitter, - $options_cape_link[$selected] - ) - ); - } - ); - } - - private function capeOptions(Player $player, $selected_cape){ - - $cape = $this->plugin->getCapes()[$selected_cape]; - $player_has_cape = $this->plugin->hasCape($player, $selected_cape); - - return new MenuForm( - $cape["name"], - $cape["description"], - [ - new MenuOption( - $player_has_cape ? "Use" : "Use §o(locked)", - new FormIcon("textures/ui/dressing_room_capes", FormIcon::IMAGE_TYPE_PATH) - ), - new MenuOption( - "Go back", - new FormIcon("textures/ui/arrow_left", FormIcon::IMAGE_TYPE_PATH) - ) - ], - function(Player $submitter, int $choice) use ($selected_cape, $player_has_cape) : void{ - - // If player go back - if($choice == 1){ - $submitter->sendForm( - $this->capesList($submitter) - ); return; - } - - $cape_name = $this->plugin->getCapes()[$selected_cape]["name"]; - - // If player doesn't have this cape - if(!$player_has_cape){ - $submitter->sendMessage(str_replace( - "{cape}", - $cape_name, - $this->locked_cape_message - )); return; - }; - - $player_cooldown = $this->players_cooldown[$submitter->getName()] ?? false; - - if( $player_cooldown ){ - $submitter->sendMessage( $this->cooldown_message ); return; - } - - $player_name = $submitter->getName(); - $this->players_cooldown[$player_name] = true; - $this->plugin->getScheduler()->scheduleDelayedTask( - new ClosureTask(function() use ($player_name) : void{ - unset($this->players_cooldown[$player_name]); - }), - $this->cooldown * 20 - ); - - $this->plugin->setPlayerCape($submitter, $selected_cape); - $submitter->sendMessage(str_replace( - "{cape}", - $cape_name, - $this->cape_equiped_message - )); - } - ); - - } - -} \ No newline at end of file diff --git a/src/Verre2OuiSki/CapesLocker/Commands/ManageCapes.php b/src/Verre2OuiSki/CapesLocker/Commands/ManageCapes.php deleted file mode 100644 index fb49c0a..0000000 --- a/src/Verre2OuiSki/CapesLocker/Commands/ManageCapes.php +++ /dev/null @@ -1,185 +0,0 @@ -plugin = $plugin; - - parent::__construct( - "managecapes", - "Manage capes of a player", - "/mcapes [cape id] [lock|unlock]", - ["mcapes"] - ); - $this->setPermission("capeslocker.command.managecapes"); - } - - public function execute(CommandSender $sender, string $commandLabel, array $args){ - - // Sender doesn't have permission to execute this command - if(!$this->testPermission($sender, $this->getPermission())) return; - - - $player = $this->plugin->getServer()->getPlayerByPrefix($args[0] ?? "😋"); - $cape_id = $args[1] ?? null; - $action_type = $args[2] ?? null; - - - // Sender isn't a player and didn't specify args OR player isn't specify - if((!$sender instanceof Player && !$action_type) || !isset($args[0])){ - $sender->sendMessage($this->getUsage()); return; - } - - // If player selected isn't connected - if(!$player){ - $sender->sendMessage("§cCan't find player : $args[0]"); return; - } - - // If sender use command - if( $action_type ){ - - $cape = $this->plugin->getCapeById($cape_id); - // If cape selected doesn't exist - if(!$cape){ - $sender->sendMessage("§cThere is no cape with this ID : $cape_id"); return; - } - - // if action isn't lock or unlock - $action_type = strtolower($action_type); - if($action_type !== "lock" && $action_type !== "unlock"){ - $sender->sendMessage("§cInvalid action : $cape_id\nuse 'lock' or 'unlock' action"); return; - } - - if($action_type === "lock"){ - $this->plugin->lockCape($player, $cape_id); - $sender->sendMessage("$cape_id has been removed from " . $player->getName() . "'s capes"); - $player->sendMessage($cape["name"] . " §rhas been removed from your capes by " . $sender->getName()); - return; - } - - $this->plugin->unlockCape($player, $cape_id); - $sender->sendMessage("$cape_id has been added to " . $player->getName() . "'s capes"); - $player->sendMessage($cape["name"] . " §rhas been added to your capes by " . $sender->getName()); - return; - } - - // Send manage capes form - if($sender instanceof Player){ - $sender->sendForm($this->playerCapeList($player)); - } - - } - - - private function playerCapeList(Player $player_capes){ - - $options = []; - $options_cape_link = []; - - $unlocked_capes = $this->plugin->getPlayerCapes($player_capes); - $locked_capes = array_diff_key($this->plugin->getCapes(), array_merge($this->plugin->getDefaultCapes(), $unlocked_capes, $this->plugin->getPlayerPermittedCapes($player_capes))); - - // Set player capes at top of the menu - foreach ($unlocked_capes as $cape_id => $cape) { - array_push( - $options, - new MenuOption( - $cape["name"], - new FormIcon("textures/ui/icon_unlocked", FormIcon::IMAGE_TYPE_PATH) - ) - ); - $options_cape_link[array_key_last($options)] = $cape_id; - - } - - // Locked capes after unlocked capes - foreach($locked_capes as $cape_id => $cape){ - - array_push( - $options, - new MenuOption( - $cape["name"], - new FormIcon("textures/ui/icon_lock", FormIcon::IMAGE_TYPE_PATH) - ) - ); - $options_cape_link[array_key_last($options)] = $cape_id; - } - - return new MenuForm( - $player_capes->getName() . "'s capes", - "", - $options, - function( Player $submitter, int $selected ) use ($options_cape_link, $player_capes) : void { - - $submitter->sendForm( - $this->capeOptions( - $player_capes, - $options_cape_link[$selected] - ) - ); - } - ); - } - - private function capeOptions(Player $player_capes, $selected_cape){ - - $cape = $this->plugin->getCapes()[$selected_cape]; - - return new MenuForm( - $cape["name"], - $cape["description"], - [ - new MenuOption( - "Remove from " . $player_capes->getName() . "'s capes", - new FormIcon("textures/ui/icon_trash", FormIcon::IMAGE_TYPE_PATH) - ), - new MenuOption( - "Add to " . $player_capes->getName() . "'s capes", - new FormIcon("textures/ui/download_backup", FormIcon::IMAGE_TYPE_PATH) - ), - new MenuOption( - "Go back", - new FormIcon("textures/ui/arrow_left", FormIcon::IMAGE_TYPE_PATH) - ) - ], - function(Player $submitter, int $choice) use ($selected_cape, $player_capes) : void{ - - // If player go back - if($choice == 2){ - $submitter->sendForm( - $this->playerCapeList($player_capes) - ); return; - } - - $cape_name = $this->plugin->getCapes()[$selected_cape]["name"]; - - // If the cape has been removed - if($choice == 0){ - - $this->plugin->lockCape($player_capes, $selected_cape); - $submitter->sendMessage("$selected_cape has been removed from " . $player_capes->getName() . "'s capes"); - $player_capes->sendMessage("$cape_name §rhas been removed from your capes by " . $submitter->getName()); - return; - } - - // If the cape has been added - $this->plugin->unlockCape($player_capes, $selected_cape); - $submitter->sendMessage("$selected_cape has been added to " . $player_capes->getName() . "'s capes"); - $player_capes->sendMessage("$cape_name §rhas been added to your capes by " . $submitter->getName()); - } - ); - } - -} \ No newline at end of file diff --git a/src/Verre2OuiSki/CapesLocker/Commands/PlayersCapesCleaner.php b/src/Verre2OuiSki/CapesLocker/Commands/PlayersCapesCleaner.php deleted file mode 100644 index 51eea76..0000000 --- a/src/Verre2OuiSki/CapesLocker/Commands/PlayersCapesCleaner.php +++ /dev/null @@ -1,46 +0,0 @@ -plugin = $plugin; - parent::__construct( - "playerscapescleaner", - "WARNING ! This command remove all undefined capes in 'capes.json' from capes lockers of players.", - "/playerscapescleaner" - ); - $this->setPermission("capeslocker.command.playerscapescleaner"); - } - - public function execute(CommandSender $sender, string $commandLabel, array $args){ - - if(!$this->testPermission($sender, $this->getPermission())) return; - - $capes = array_keys($this->plugin->getCapes()); - $players_capes = $this->plugin->getPlayersCapes()->getAll(); - - foreach ($players_capes as $player_uuid => $capes_id) { - foreach($capes_id as $cape_id){ - if( !in_array($cape_id, $capes) ){ - unset( - $players_capes[$player_uuid][array_search($cape_id, $players_capes[$player_uuid])] - ); - } - } - $players_capes[$player_uuid] = array_values($players_capes[$player_uuid]); - } - - $this->plugin->getPlayersCapes()->reload(); - $this->plugin->getPlayersCapes()->setAll($players_capes); - $this->plugin->getPlayersCapes()->save(); - $sender->sendMessage("§cplayers_capes.yml has been cleaned up"); - } -} \ No newline at end of file diff --git a/src/Verre2OuiSki/CapesLocker/Listeners/SetCape.php b/src/Verre2OuiSki/CapesLocker/Listeners/SetCape.php deleted file mode 100644 index 292a50a..0000000 --- a/src/Verre2OuiSki/CapesLocker/Listeners/SetCape.php +++ /dev/null @@ -1,35 +0,0 @@ -plugin = $plugin; - } - - - // Set cape if player disconnect with a cape - public function onPlayerJoin( PlayerJoinEvent $event ){ - - $player = $event->getPlayer(); - $cape = $this->plugin->getWearingCapeId( $player ); - - $wearing_capes = $this->plugin->getWearingCapes(); - - if( !$this->plugin->getCapeById( $cape ) ) { - $wearing_capes->remove($player->getUniqueId()->toString()); - $wearing_capes->save(); - return; - } - - if($cape) $this->plugin->setPlayerCape( $player, $cape ); - } - -} \ No newline at end of file diff --git a/src/Verre2OuiSki/CapesLocker/libs/dktapps/pmforms/BaseForm.php b/src/Verre2OuiSki/CapesLocker/libs/dktapps/pmforms/BaseForm.php deleted file mode 100644 index bce3f63..0000000 --- a/src/Verre2OuiSki/CapesLocker/libs/dktapps/pmforms/BaseForm.php +++ /dev/null @@ -1,73 +0,0 @@ -title = $title; - } - - /** - * Returns the text shown on the form title-bar. - */ - public function getTitle() : string{ - return $this->title; - } - - /** - * Serializes the form to JSON for sending to clients. - * @return mixed[] - */ - final public function jsonSerialize() : array{ - $ret = $this->serializeFormData(); - $ret["type"] = $this->getType(); - $ret["title"] = $this->getTitle(); - - return $ret; - } - - /** - * Returns the type used to show this form to clients - */ - abstract protected function getType() : string; - - /** - * Serializes additional data needed to show this form to clients. - * @return mixed[] - */ - abstract protected function serializeFormData() : array; - -} diff --git a/src/Verre2OuiSki/CapesLocker/libs/dktapps/pmforms/CustomForm.php b/src/Verre2OuiSki/CapesLocker/libs/dktapps/pmforms/CustomForm.php deleted file mode 100644 index 23ba430..0000000 --- a/src/Verre2OuiSki/CapesLocker/libs/dktapps/pmforms/CustomForm.php +++ /dev/null @@ -1,139 +0,0 @@ -elements = array_values($elements); - foreach($this->elements as $element){ - if(isset($this->elementMap[$element->getName()])){ - throw new \InvalidArgumentException("Multiple elements cannot have the same name, found \"" . $element->getName() . "\" more than once"); - } - $this->elementMap[$element->getName()] = $element; - } - - Utils::validateCallableSignature(function(Player $player, CustomFormResponse $response) : void{}, $onSubmit); - $this->onSubmit = $onSubmit; - if($onClose !== null){ - Utils::validateCallableSignature(function(Player $player) : void{}, $onClose); - $this->onClose = $onClose; - } - } - - public function getElement(int $index) : ?CustomFormElement{ - return $this->elements[$index] ?? null; - } - - public function getElementByName(string $name) : ?CustomFormElement{ - return $this->elementMap[$name] ?? null; - } - - /** - * @return CustomFormElement[] - */ - public function getAllElements() : array{ - return $this->elements; - } - - final public function handleResponse(Player $player, $data) : void{ - if($data === null){ - if($this->onClose !== null){ - ($this->onClose)($player); - } - }elseif(is_array($data)){ - if(($actual = count($data)) !== ($expected = count($this->elements))){ - throw new FormValidationException("Expected $expected result data, got $actual"); - } - - $values = []; - - foreach($data as $index => $value){ - if(!isset($this->elements[$index])){ - throw new FormValidationException("Element at offset $index does not exist"); - } - $element = $this->elements[$index]; - try{ - $element->validateValue($value); - }catch(FormValidationException $e){ - throw new FormValidationException("Validation failed for element \"" . $element->getName() . "\": " . $e->getMessage(), 0, $e); - } - $values[$element->getName()] = $value; - } - - ($this->onSubmit)($player, new CustomFormResponse($values)); - }else{ - throw new FormValidationException("Expected array or null, got " . gettype($data)); - } - } - - protected function getType() : string{ - return "custom_form"; - } - - protected function serializeFormData() : array{ - return [ - "content" => $this->elements - ]; - } -} diff --git a/src/Verre2OuiSki/CapesLocker/libs/dktapps/pmforms/CustomFormResponse.php b/src/Verre2OuiSki/CapesLocker/libs/dktapps/pmforms/CustomFormResponse.php deleted file mode 100644 index ef4296e..0000000 --- a/src/Verre2OuiSki/CapesLocker/libs/dktapps/pmforms/CustomFormResponse.php +++ /dev/null @@ -1,77 +0,0 @@ - - */ -class CustomFormResponse{ - /** - * @var mixed[] - * @phpstan-var ResponseData - */ - private $data; - - /** - * @param mixed[] $data - * @phpstan-param ResponseData $data - */ - public function __construct(array $data){ - $this->data = $data; - } - - public function getInt(string $name) : int{ - $this->checkExists($name); - return $this->data[$name]; - } - - public function getString(string $name) : string{ - $this->checkExists($name); - return $this->data[$name]; - } - - public function getFloat(string $name) : float{ - $this->checkExists($name); - return $this->data[$name]; - } - - public function getBool(string $name) : bool{ - $this->checkExists($name); - return $this->data[$name]; - } - - /** - * @return mixed[] - * @phpstan-return ResponseData - */ - public function getAll() : array{ - return $this->data; - } - - private function checkExists(string $name) : void{ - if(!isset($this->data[$name])){ - throw new \InvalidArgumentException("Value \"$name\" not found"); - } - } -} diff --git a/src/Verre2OuiSki/CapesLocker/libs/dktapps/pmforms/FormIcon.php b/src/Verre2OuiSki/CapesLocker/libs/dktapps/pmforms/FormIcon.php deleted file mode 100644 index 1f0c44d..0000000 --- a/src/Verre2OuiSki/CapesLocker/libs/dktapps/pmforms/FormIcon.php +++ /dev/null @@ -1,61 +0,0 @@ -type = $type; - $this->data = $data; - } - - public function getType() : string{ - return $this->type; - } - - public function getData() : string{ - return $this->data; - } - - public function jsonSerialize(){ - return [ - "type" => $this->type, - "data" => $this->data - ]; - } -} diff --git a/src/Verre2OuiSki/CapesLocker/libs/dktapps/pmforms/MenuForm.php b/src/Verre2OuiSki/CapesLocker/libs/dktapps/pmforms/MenuForm.php deleted file mode 100644 index 9edd97c..0000000 --- a/src/Verre2OuiSki/CapesLocker/libs/dktapps/pmforms/MenuForm.php +++ /dev/null @@ -1,106 +0,0 @@ -content = $text; - $this->options = array_values($options); - Utils::validateCallableSignature(function(Player $player, int $selectedOption) : void{}, $onSubmit); - $this->onSubmit = $onSubmit; - if($onClose !== null){ - Utils::validateCallableSignature(function(Player $player) : void{}, $onClose); - $this->onClose = $onClose; - } - } - - public function getOption(int $position) : ?MenuOption{ - return $this->options[$position] ?? null; - } - - final public function handleResponse(Player $player, $data) : void{ - if($data === null){ - if($this->onClose !== null){ - ($this->onClose)($player); - } - }elseif(is_int($data)){ - if(!isset($this->options[$data])){ - throw new FormValidationException("Option $data does not exist"); - } - ($this->onSubmit)($player, $data); - }else{ - throw new FormValidationException("Expected int or null, got " . gettype($data)); - } - } - - protected function getType() : string{ - return "form"; - } - - protected function serializeFormData() : array{ - return [ - "content" => $this->content, - "buttons" => $this->options //yes, this is intended (MCPE calls them buttons) - ]; - } -} diff --git a/src/Verre2OuiSki/CapesLocker/libs/dktapps/pmforms/MenuOption.php b/src/Verre2OuiSki/CapesLocker/libs/dktapps/pmforms/MenuOption.php deleted file mode 100644 index 676170f..0000000 --- a/src/Verre2OuiSki/CapesLocker/libs/dktapps/pmforms/MenuOption.php +++ /dev/null @@ -1,64 +0,0 @@ -text = $text; - $this->image = $image; - } - - public function getText() : string{ - return $this->text; - } - - public function hasImage() : bool{ - return $this->image !== null; - } - - public function getImage() : ?FormIcon{ - return $this->image; - } - - public function jsonSerialize(){ - $json = [ - "text" => $this->text - ]; - - if($this->hasImage()){ - $json["image"] = $this->image; - } - - return $json; - } -} diff --git a/src/Verre2OuiSki/CapesLocker/libs/dktapps/pmforms/ModalForm.php b/src/Verre2OuiSki/CapesLocker/libs/dktapps/pmforms/ModalForm.php deleted file mode 100644 index e301f15..0000000 --- a/src/Verre2OuiSki/CapesLocker/libs/dktapps/pmforms/ModalForm.php +++ /dev/null @@ -1,96 +0,0 @@ -content = $text; - Utils::validateCallableSignature(function(Player $player, bool $choice) : void{}, $onSubmit); - $this->onSubmit = $onSubmit; - $this->button1 = $yesButtonText; - $this->button2 = $noButtonText; - } - - public function getYesButtonText() : string{ - return $this->button1; - } - - public function getNoButtonText() : string{ - return $this->button2; - } - - final public function handleResponse(Player $player, $data) : void{ - if(!is_bool($data)){ - throw new FormValidationException("Expected bool, got " . gettype($data)); - } - - ($this->onSubmit)($player, $data); - } - - protected function getType() : string{ - return "modal"; - } - - protected function serializeFormData() : array{ - return [ - "content" => $this->content, - "button1" => $this->button1, - "button2" => $this->button2 - ]; - } -} diff --git a/src/Verre2OuiSki/CapesLocker/libs/dktapps/pmforms/ServerSettingsForm.php b/src/Verre2OuiSki/CapesLocker/libs/dktapps/pmforms/ServerSettingsForm.php deleted file mode 100644 index 1754ae5..0000000 --- a/src/Verre2OuiSki/CapesLocker/libs/dktapps/pmforms/ServerSettingsForm.php +++ /dev/null @@ -1,59 +0,0 @@ -icon = $icon; - } - - public function hasIcon() : bool{ - return $this->icon !== null; - } - - public function getIcon() : ?FormIcon{ - return $this->icon; - } - - protected function serializeFormData() : array{ - $data = parent::serializeFormData(); - - if($this->hasIcon()){ - $data["icon"] = $this->icon; - } - - return $data; - } -} diff --git a/src/Verre2OuiSki/CapesLocker/libs/dktapps/pmforms/element/BaseSelector.php b/src/Verre2OuiSki/CapesLocker/libs/dktapps/pmforms/element/BaseSelector.php deleted file mode 100644 index 3b5ad93..0000000 --- a/src/Verre2OuiSki/CapesLocker/libs/dktapps/pmforms/element/BaseSelector.php +++ /dev/null @@ -1,82 +0,0 @@ -options = array_values($options); - - if(!isset($this->options[$defaultOptionIndex])){ - throw new \InvalidArgumentException("No option at index $defaultOptionIndex, cannot set as default"); - } - $this->defaultOptionIndex = $defaultOptionIndex; - } - - public function validateValue($value) : void{ - if(!is_int($value)){ - throw new FormValidationException("Expected int, got " . gettype($value)); - } - if(!isset($this->options[$value])){ - throw new FormValidationException("Option $value does not exist"); - } - } - - /** - * Returns the text of the option at the specified index, or null if it doesn't exist. - */ - public function getOption(int $index) : ?string{ - return $this->options[$index] ?? null; - } - - public function getDefaultOptionIndex() : int{ - return $this->defaultOptionIndex; - } - - public function getDefaultOption() : string{ - return $this->options[$this->defaultOptionIndex]; - } - - /** - * @return string[] - */ - public function getOptions() : array{ - return $this->options; - } -} diff --git a/src/Verre2OuiSki/CapesLocker/libs/dktapps/pmforms/element/CustomFormElement.php b/src/Verre2OuiSki/CapesLocker/libs/dktapps/pmforms/element/CustomFormElement.php deleted file mode 100644 index 6a21cd1..0000000 --- a/src/Verre2OuiSki/CapesLocker/libs/dktapps/pmforms/element/CustomFormElement.php +++ /dev/null @@ -1,87 +0,0 @@ -name = $name; - $this->text = $text; - } - - /** - * Returns the type of element. - */ - abstract public function getType() : string; - - /** - * Returns the element's name. This is used to identify the element in code. - */ - public function getName() : string{ - return $this->name; - } - - /** - * Returns the element's label. Usually this is used to explain to the user what a control does. - */ - public function getText() : string{ - return $this->text; - } - - /** - * Validates that the given value is of the correct type and fits the constraints for the component. This function - * should do appropriate type checking and throw whatever errors necessary if the value is not valid. - * - * @param mixed $value - * @throws FormValidationException - */ - abstract public function validateValue($value) : void; - - /** - * Returns an array of properties which can be serialized to JSON for sending. - * @return mixed[] - */ - final public function jsonSerialize() : array{ - $ret = $this->serializeElementData(); - $ret["type"] = $this->getType(); - $ret["text"] = $this->getText(); - - return $ret; - } - - /** - * Returns an array of extra data needed to serialize this element to JSON for showing to a player on a form. - * @return mixed[] - */ - abstract protected function serializeElementData() : array; -} diff --git a/src/Verre2OuiSki/CapesLocker/libs/dktapps/pmforms/element/Dropdown.php b/src/Verre2OuiSki/CapesLocker/libs/dktapps/pmforms/element/Dropdown.php deleted file mode 100644 index d3c0616..0000000 --- a/src/Verre2OuiSki/CapesLocker/libs/dktapps/pmforms/element/Dropdown.php +++ /dev/null @@ -1,38 +0,0 @@ - $this->options, - "default" => $this->defaultOptionIndex - ]; - } -} diff --git a/src/Verre2OuiSki/CapesLocker/libs/dktapps/pmforms/element/Input.php b/src/Verre2OuiSki/CapesLocker/libs/dktapps/pmforms/element/Input.php deleted file mode 100644 index 5fa5223..0000000 --- a/src/Verre2OuiSki/CapesLocker/libs/dktapps/pmforms/element/Input.php +++ /dev/null @@ -1,77 +0,0 @@ -hint = $hintText; - $this->default = $defaultText; - } - - public function getType() : string{ - return "input"; - } - - public function validateValue($value) : void{ - if(!is_string($value)){ - throw new FormValidationException("Expected string, got " . gettype($value)); - } - } - - /** - * Returns the text shown in the text-box when the box is not focused and there is no text in it. - */ - public function getHintText() : string{ - return $this->hint; - } - - /** - * Returns the text which will be in the text-box by default. - */ - public function getDefaultText() : string{ - return $this->default; - } - - protected function serializeElementData() : array{ - return [ - "placeholder" => $this->hint, - "default" => $this->default - ]; - } -} diff --git a/src/Verre2OuiSki/CapesLocker/libs/dktapps/pmforms/element/Label.php b/src/Verre2OuiSki/CapesLocker/libs/dktapps/pmforms/element/Label.php deleted file mode 100644 index 468ab02..0000000 --- a/src/Verre2OuiSki/CapesLocker/libs/dktapps/pmforms/element/Label.php +++ /dev/null @@ -1,44 +0,0 @@ -min > $this->max){ - throw new \InvalidArgumentException("Slider min value should be less than max value"); - } - $this->min = $min; - $this->max = $max; - - if($default !== null){ - if($default > $this->max or $default < $this->min){ - throw new \InvalidArgumentException("Default must be in range $this->min ... $this->max"); - } - $this->default = $default; - }else{ - $this->default = $this->min; - } - - if($step <= 0){ - throw new \InvalidArgumentException("Step must be greater than zero"); - } - $this->step = $step; - } - - public function getType() : string{ - return "slider"; - } - - public function validateValue($value) : void{ - if(!is_float($value) and !is_int($value)){ - throw new FormValidationException("Expected float, got " . gettype($value)); - } - if($value < $this->min or $value > $this->max){ - throw new FormValidationException("Value $value is out of bounds (min $this->min, max $this->max)"); - } - } - - public function getMin() : float{ - return $this->min; - } - - public function getMax() : float{ - return $this->max; - } - - public function getStep() : float{ - return $this->step; - } - - public function getDefault() : float{ - return $this->default; - } - - protected function serializeElementData() : array{ - return [ - "min" => $this->min, - "max" => $this->max, - "default" => $this->default, - "step" => $this->step - ]; - } -} diff --git a/src/Verre2OuiSki/CapesLocker/libs/dktapps/pmforms/element/StepSlider.php b/src/Verre2OuiSki/CapesLocker/libs/dktapps/pmforms/element/StepSlider.php deleted file mode 100644 index 217d9eb..0000000 --- a/src/Verre2OuiSki/CapesLocker/libs/dktapps/pmforms/element/StepSlider.php +++ /dev/null @@ -1,38 +0,0 @@ - $this->options, - "default" => $this->defaultOptionIndex - ]; - } -} diff --git a/src/Verre2OuiSki/CapesLocker/libs/dktapps/pmforms/element/Toggle.php b/src/Verre2OuiSki/CapesLocker/libs/dktapps/pmforms/element/Toggle.php deleted file mode 100644 index bf4d199..0000000 --- a/src/Verre2OuiSki/CapesLocker/libs/dktapps/pmforms/element/Toggle.php +++ /dev/null @@ -1,61 +0,0 @@ -default = $defaultValue; - } - - public function getType() : string{ - return "toggle"; - } - - public function getDefaultValue() : bool{ - return $this->default; - } - - public function validateValue($value) : void{ - if(!is_bool($value)){ - throw new FormValidationException("Expected bool, got " . gettype($value)); - } - } - - protected function serializeElementData() : array{ - return [ - "default" => $this->default - ]; - } -}