forked from Ad5001/PlayerSelectors
First commit!
This commit is contained in:
commit
e02535809b
12 changed files with 718 additions and 0 deletions
113
LICENSE.md
Normal file
113
LICENSE.md
Normal file
|
@ -0,0 +1,113 @@
|
||||||
|
Not Truly Open Source License
|
||||||
|
=========================================
|
||||||
|
This license is an adaption of the Creative Commons Licenses for softwares.
|
||||||
|
Any user using and, or Distributing the Software (defined in I) exerces the Licensed Rights.
|
||||||
|
By exercising the Licenced Rights (defined below), You accept and agree to be bound by the terms and conditions of this Not Truly Open Source ("NTOS License"). To the extent the NTOS License may be interpreted as a contract, You are granted the Licenced Rights in consideration of Your acceptance of these terms and conditions, and the Author grants You such rights in consideration of benefits the Author receives from making the Software available under these terms and conditions.
|
||||||
|
|
||||||
|
I. Definitions:
|
||||||
|
- "NTOS License" refers to the legal terms of this license, described here.
|
||||||
|
- "Licensed Rights" refers the rights granted to You subject to the terms and conditions of the NTOS License, which are limited to all Copyright and Similar Rights that apply to Your use of the Software and that the Author has authority to license.
|
||||||
|
- "The Software" refers to any work, code and software binaries licensed under the NTOS License.
|
||||||
|
- "The Author" refers the orginal author of the Software, the one that licenced the Software under the NTOS License.
|
||||||
|
- "the User", "User", "You", and "Your" refers the individual or entity exercising the Licenced Rights under the NTOS License.
|
||||||
|
- "the Modification" refers to based software of the Software including more than 10% of the Software's code.
|
||||||
|
- "the Modification's Author" refers to an User that Distributes the Modificaction
|
||||||
|
- "Code" refers to any part of the code located in the Software.
|
||||||
|
- "the Based Software" refers to work other than the Software using the Code. The used Code must not be larger than 10% of the code of the Software. Otherwise, the work is a Modification.
|
||||||
|
- "the Based Software's Author" refers to an User that created the
|
||||||
|
- "The End User" refers to the individual person accessing and, or using the Software. The End User may be or not the same as the User.
|
||||||
|
- "Exceptions and Limitations" refers to the fair use, fair dealing, and/or any other exception or limitation to Copyright and Similar Rights that applies to Your use of the Software.
|
||||||
|
- "Copyright and Similar Rights" refers to the copyright and/or similar rights closely related to copyright including, without limitation, performance, broadcast, sound recording, and Sui Generis Database Rights, without regard to how the Licenced Rights are labeled or categorized. For purposes of the NTOS License, the Licenced Rights specified in II(2)(a)-(b) are not Copyright and Similar Rights.
|
||||||
|
- "Distribute" refers to any way of providing The End User an access to the Software either by a downloading Platform or from any internet connected process allowing the user to access the Software content.
|
||||||
|
- "NonCommercial" means not primarily intended for or directed towards commercial advantage or monetary compensation. For purposes of the NTOS License, the exchange of the Software for other material subject to Copyright and Similar Rights by digital file-sharing or similar means is NonCommercial provided there is no payment of monetary compensation in connection with the exchange.
|
||||||
|
- "Sui Generis Database Rights" refers to the Licenced Rights other than copyright resulting from Directive 96/9/EC of the European Parliament and of the Council of 11 March 1996 on the legal protection of databases, as amended and/or succeeded, as well as other essentially equivalent rights anywhere in the world.
|
||||||
|
|
||||||
|
Section II. Scope.
|
||||||
|
|
||||||
|
1. License grant.
|
||||||
|
a. Subject to the terms and conditions of the NTOS License, the Author hereby grants You a worldwide, royalty-free, non-sublicensable, non-exclusive, irrevocable license to exercise the Licenced Rights in the Software to:
|
||||||
|
A. reproduce and Distribute the Software, in whole or in part, for NonCommercial purposes only; and
|
||||||
|
B. produce, reproduce, and Distribute Modification for NonCommercial purposes only.
|
||||||
|
b. Exceptions and Limitations. For the avoidance of doubt, where Exceptions and Limitations apply to Your use, the NTOS License does not apply, and You do not need to comply with its terms and conditions.
|
||||||
|
c. Term. The term of the NTOS License is specified in Section VI(1).
|
||||||
|
d. Downstream recipients.
|
||||||
|
Offer from the Author – Software. Every recipient of the Software automatically receives an offer from the Author to exercise the Licenced Rights under the terms and conditions of the NTOS License.
|
||||||
|
Additional offer from the Author – Modification. Every recipient of Modification from You automatically receives an offer from the Author to exercise the Licenced Rights in the Modification under the conditions of the the Modification's Author's License You apply.
|
||||||
|
No downstream restrictions. You may not offer or impose any additional or different terms or conditions on, or apply any Effective Technological Measures to, the Software if doing so restricts exercise of the Licenced Rights by any recipient of the Software.
|
||||||
|
e. No endorsement. Nothing in the NTOS License constitutes or may be construed as permission to assert or imply that You are, or that Your use of the Software is, connected with, or sponsored, endorsed, or granted official status by, the Author or others designated to receive attribution as provided in Section III(1)(a)(A)(i).
|
||||||
|
2. Other rights.
|
||||||
|
a. Moral rights, such as the right of integrity, are not licensed under the NTOS License, nor are publicity, privacy, and/or other similar personality rights; however, to the extent possible, the Author waives and/or agrees not to assert any such rights held by the Author to the limited extent necessary to allow You to exercise the Licenced Rights, but not otherwise.
|
||||||
|
b. Patent and trademark rights are not licensed under the NTOS License.
|
||||||
|
c. To the extent possible, the Author waives any right to collect royalties from You for the exercise of the Licenced Rights, whether directly or through a collecting society under any voluntary or waivable statutory or compulsory licensing scheme. In all other cases the Author expressly reserves any right to collect such royalties, including when the Software is used other than for NonCommercial purposes.
|
||||||
|
|
||||||
|
Section III. License Conditions.
|
||||||
|
Your exercise of the Licensed Rights is expressly made subject to the following conditions.
|
||||||
|
|
||||||
|
1. Attribution.
|
||||||
|
a. If You Distribute the Software (including in modified, or based form), You must:
|
||||||
|
A. retain the following if it is supplied by the Author with the Software:
|
||||||
|
i. identification of the creator(s) of the Software and any others designated to receive attribution, in any reasonable manner requested by the Author (including by pseudonym if designated);
|
||||||
|
ii. a copyright notice;
|
||||||
|
iii. a notice that refers to the NTOS License;
|
||||||
|
iv. a notice that refers to the disclaimer of warranties;
|
||||||
|
v. a URI or hyperlink to the Software to the extent reasonably practicable;
|
||||||
|
indicate if You modified the Software and retain an indication of any previous modifications; and
|
||||||
|
indicate the Software is licensed under the NTOS License, and include the text of, or the URI or hyperlink to, the NTOS License.
|
||||||
|
b. You may satisfy the conditions in Section III(1)(a) in any reasonable manner based on the medium, means, and context in which You Distribute the Software. For example, it may be reasonable to satisfy the conditions by providing a URI or hyperlink to a resource that includes the required information.
|
||||||
|
c. If requested by the Author, You must remove any of the information required by Section III(1)(a)(A) to the extent reasonably practicable.
|
||||||
|
|
||||||
|
2. Distribute ALike
|
||||||
|
In addition to the conditions in Section III(1), if You Distribute the Modification You produce, the following conditions also apply.
|
||||||
|
A. The Modification Author's License You apply must be the NTOS License
|
||||||
|
B. You must include the text of, or the URI or hyperlink to, the Modification's Author's License You apply. You may satisfy this condition in any reasonable manner based on the medium, means, and context in which You Distribute the Modification.
|
||||||
|
C. You may not offer or impose any additional or different terms or conditions on, or apply any Effective Technological Measures to, the Modification that restrict exercise of the rights granted under the Modification's Author's License You apply.
|
||||||
|
D. You may not apply any fee to access the Modification
|
||||||
|
|
||||||
|
3. Distribution of code
|
||||||
|
In addition to the conditions in Section III(1), if You Distribute a Based Software, the following conditions also apply:
|
||||||
|
A. You must include the text of, or the URI or hyperlink to, refering to this License. You may satisfy this condition in any reasonable manner based on the medium, means, and context in which You Distribute the Based Software.
|
||||||
|
B. You must also include Section III(1)(a)(A)(ii-v) in the the Based Software Code.
|
||||||
|
|
||||||
|
Section IV. Sui Generis Database Rights.
|
||||||
|
|
||||||
|
Where the Licensed Rights include Sui Generis Database Rights that apply to Your use of the Software:
|
||||||
|
|
||||||
|
for the avoidance of doubt, Section II(1)(a) grants You the right to extract, reuse, reproduce, and Dstribute all or a substantial portion of the contents of the database for NonCommercial purposes only;
|
||||||
|
if You include all or a substantial portion of the database contents in a database in which You have Sui Generis Database Rights, then the database in which You have Sui Generis Database Rights (but not its individual contents) is the Modification, including for purposes of Section III(2); and
|
||||||
|
You must comply with the conditions in Section III(1) if You Distribute all or a substantial portion of the contents of the database.
|
||||||
|
|
||||||
|
For the avoidance of doubt, this Section IV supplements and does not replace Your obligations under the NTOS License where the Licensed Rights include other Copyright and Similar Rights.
|
||||||
|
|
||||||
|
|
||||||
|
Section V – Disclaimer of Warranties and Limitation of Liability.
|
||||||
|
|
||||||
|
1. Unless otherwise separately undertaken by the Author, to the extent possible, the Author offers the Software as-is and as-available, and makes no representations or warranties of any kind concerning the Software, whether express, implied, statutory, or other. This includes, without limitation, warranties of title, merchantability, fitness for a particular purpose, non-infringement, absence of latent or other defects, accuracy, or the presence or absence of errors, whether or not known or discoverable. Where disclaimers of warranties are not allowed in full or in part, this disclaimer may not apply to You.
|
||||||
|
2. To the extent possible, in no event will the Author be liable to You on any legal theory (including, without limitation, negligence) or otherwise for any direct, special, indirect, incidental, consequential, punitive, exemplary, or other losses, costs, expenses, or damages arising out of the NTOS License or use of the Software, even if the Author has been advised of the possibility of such losses, costs, expenses, or damages. Where a limitation of liability is not allowed in full or in part, this limitation may not apply to You.
|
||||||
|
|
||||||
|
3. The disclaimer of warranties and limitation of liability provided above shall be interpreted in a manner that, to the extent possible, most closely approximates an absolute disclaimer and waiver of all liability.
|
||||||
|
|
||||||
|
|
||||||
|
Section VI – Term and Termination.
|
||||||
|
|
||||||
|
The NTOS License applies for the term of the Copyright and Similar Rights licensed here. However, if You fail to comply with the NTOS License, then Your rights under the NTOS License terminate automatically.
|
||||||
|
|
||||||
|
Where Your right to use the Software has terminated under Section 6(a), it reinstates:
|
||||||
|
automatically as of the date the violation is cured, provided it is cured within 30 days of Your discovery of the violation; or
|
||||||
|
upon express reinstatement by the Author.
|
||||||
|
For the avoidance of doubt, this Section 6(b) does not affect any right the Author may have to seek remedies for Your violations of the NTOS License.
|
||||||
|
For the avoidance of doubt, the Author may also offer the Software under separate terms or conditions or stop distributing the Software at any time; however, doing so will not terminate the NTOS License.
|
||||||
|
Sections I, V, VI, VII, and VI survive termination of the NTOS License.
|
||||||
|
|
||||||
|
Section VII – Other Terms and Conditions.
|
||||||
|
|
||||||
|
The Licensor shall not be bound by any additional or different terms or conditions communicated by You unless expressly agreed.
|
||||||
|
Any arrangements, understandings, or agreements regarding the Software not stated herein are separate from and independent of the terms and conditions of the NTOS License.
|
||||||
|
|
||||||
|
Section VIII – Interpretation.
|
||||||
|
|
||||||
|
For the avoidance of doubt, the NTOS License does not, and shall not be interpreted to, reduce, limit, restrict, or impose conditions on any use of the Software that could lawfully be made without permission under the NTOS License.
|
||||||
|
To the extent possible, if any provision of the NTOS License is deemed unenforceable, it shall be automatically reformed to the minimum extent necessary to make it enforceable. If the provision cannot be reformed, it shall be severed from the NTOS License without affecting the enforceability of the remaining terms and conditions.
|
||||||
|
No term or condition of the NTOS License will be waived and no failure to comply consented to unless expressly agreed to by the Author.
|
||||||
|
Nothing in the NTOS License constitutes or may be interpreted as a limitation upon, or waiver of, any privileges and immunities that apply to the Author or You, including from the legal processes of any jurisdiction or authority.
|
||||||
|
|
||||||
|
In case of a specific issue not specified in the NTOSL, Berne Convention for the Protection of Literary and Artistic Works, which reserves all rights to the Author applies. You can find Berne Convention for the Protection of Literary and Artistic Works at http://www.wipo.int/treaties/en/text.jsp?file_id=283698.
|
28
README.md
Normal file
28
README.md
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
<h1><img src="https://github.com/Ad5001/PlayerSelectors/raw/master/icon.png"> PlayerSelectors</h1>
|
||||||
|
Implementation of minecraft selectors into PocketMine!
|
||||||
|
|
||||||
|
<h1><img src="https://png.icons8.com/?id=365&size=1x"> How to install?</h1>
|
||||||
|
1. Download the .phar file from the link given at the top of the repo
|
||||||
|
2. Put that phar into your "plugins" folder
|
||||||
|
3. Restart your server and Selectors should be reay to go!
|
||||||
|
|
||||||
|
<h1><img src="https://png.icons8.com/?id=3330&size=1x"> Implemented features</h1>
|
||||||
|
PlayerSelectors implements all the default minecraft selectors:<br>
|
||||||
|
- @a - All players<br>
|
||||||
|
- @p - Nearset player<br>
|
||||||
|
- @e - Entities<br>
|
||||||
|
- @r - Random player<br>
|
||||||
|
- @s - Self/Sender<br>
|
||||||
|
but also features a custom made one:<br>
|
||||||
|
Since PocketMine implements a multi world system, you can easily select players from your current world using the "@w" selector!
|
||||||
|
<br><br>
|
||||||
|
Minecraft also supports specific search parameters such as the distance from the sender, the count of matched player/entities, ...<br>
|
||||||
|
All the standard minecraft search params can be used with the syntax
|
||||||
|
|
||||||
|
```
|
||||||
|
@<selector name>[key=param,other=value]
|
||||||
|
```
|
||||||
|
|
||||||
|
EXEPT the team & score params since the /scoreboard command isn't implemeted into pocketmine yet<br> Thought, one new params comes with this plugin: the "lvl" param which is for searching in a specific level.
|
||||||
|
|
||||||
|
Have fun!
|
BIN
icon.png
Normal file
BIN
icon.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 5.5 KiB |
9
plugin.yml
Normal file
9
plugin.yml
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
---
|
||||||
|
name: PlayerSelectors
|
||||||
|
author: Ad5001
|
||||||
|
version: 1.0
|
||||||
|
api: [3.0.0-ALPHA9]
|
||||||
|
main: Ad5001\PlayerSelectors\Main
|
||||||
|
commands: []
|
||||||
|
permissions: []
|
||||||
|
...
|
180
src/Ad5001/PlayerSelectors/Main.php
Normal file
180
src/Ad5001/PlayerSelectors/Main.php
Normal file
|
@ -0,0 +1,180 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Ad5001\PlayerSelectors;
|
||||||
|
|
||||||
|
use pocketmine\plugin\PluginBase;
|
||||||
|
use pocketmine\event\Listener;
|
||||||
|
use pocketmine\Player;
|
||||||
|
use pocketmine\Server;
|
||||||
|
use pocketmine\command\CommandSender;
|
||||||
|
use pocketmine\event\player\PlayerCommandPreprocessEvent;
|
||||||
|
use pocketmine\event\server\ServerCommandEvent;
|
||||||
|
|
||||||
|
|
||||||
|
use Ad5001\PlayerSelectors\selector\Selector;
|
||||||
|
use Ad5001\PlayerSelectors\selector\ClosestPlayer;
|
||||||
|
use Ad5001\PlayerSelectors\selector\AllPlayers;
|
||||||
|
use Ad5001\PlayerSelectors\selector\RandomPlayer;
|
||||||
|
use Ad5001\PlayerSelectors\selector\WorldPlayers;
|
||||||
|
use Ad5001\PlayerSelectors\selector\Entities;
|
||||||
|
use Ad5001\PlayerSelectors\selector\SelfSelector;
|
||||||
|
|
||||||
|
|
||||||
|
class Main extends PluginBase implements Listener {
|
||||||
|
|
||||||
|
protected static $selectors = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* When the plugin enables
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function onEnable(): void{
|
||||||
|
$this->getServer()->getPluginManager()->registerEvents($this, $this);
|
||||||
|
// Registering the default selectors
|
||||||
|
self::registerSelector(new ClosestPlayer());
|
||||||
|
self::registerSelector(new AllPlayers());
|
||||||
|
self::registerSelector(new RandomPlayer());
|
||||||
|
self::registerSelector(new WorldPlayers());
|
||||||
|
self::registerSelector(new Entities());
|
||||||
|
self::registerSelector(new SelfSelector());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* When a command is executed, check for selectors
|
||||||
|
*
|
||||||
|
* @param PlayerCommandPreProcessEvent $event
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function onCommandPreProcess(PlayerCommandPreProcessEvent $event): void{
|
||||||
|
$m = $event->getMessage();
|
||||||
|
$this->execSelectors($m, $event->getPlayer());
|
||||||
|
$event->setCancelled();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* When a command is executed, check for selectors
|
||||||
|
*
|
||||||
|
* @param PlayerCommandPreProcessEvent $event
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function onServerCommand(ServerCommandEvent $event): void{
|
||||||
|
$m = $event->getCommand();
|
||||||
|
$this->execSelectors($m, $event->getSender());
|
||||||
|
$event->setCancelled();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses selectors and executes the commands
|
||||||
|
*
|
||||||
|
* @param string $m The command
|
||||||
|
* @param CommandSender $sender
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function execSelectors(string $m, CommandSender $sender): void{
|
||||||
|
preg_match_all($this->buildRegExr(), $m, $matches);
|
||||||
|
$commandsToExecute = [$m];
|
||||||
|
foreach($matches[0] as $index => $match){
|
||||||
|
if(isset(self::$selectors[$matches[1][$index]])){ // Does the selector exist?
|
||||||
|
// Search for the parameters
|
||||||
|
echo $matches[1][$index];
|
||||||
|
$params = self::$selectors[$matches[1][$index]]->acceptsModifiers() ? $this->checkArgParams($matches, $index): [];
|
||||||
|
// Applying the selector
|
||||||
|
$newCommandsToExecute = [];
|
||||||
|
foreach($commandsToExecute as $index => $cmd){
|
||||||
|
// Foreaching the returning commands to push them to the new commands to be executed at the next run.
|
||||||
|
foreach(self::$selectors[$matches[1][$index]]->applySelector($sender, $params) as $selectorStr){
|
||||||
|
$newCommandsToExecute[] = substr_replace($cmd, " " . $selectorStr . " ", strpos($cmd, $match), strlen($match));
|
||||||
|
}
|
||||||
|
if(count($newCommandsToExecute) == 0) {
|
||||||
|
$sender->sendMessage("§cYour selector $match (" . self::$selectors[$matches[1][$index]]->getName() . ") did not mactch any player/entity.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$commandsToExecute = $newCommandsToExecute;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Then we have all the commands here and we can execute them
|
||||||
|
foreach($commandsToExecute as $cmd){
|
||||||
|
$this->getServer()->dispatchCommand($sender, $cmd);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return all the params in an array form in a match.
|
||||||
|
*
|
||||||
|
* @param array $match
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function checkArgParams(array $match, int $index): array{
|
||||||
|
$params = [];
|
||||||
|
if(strlen($match[2][$index]) !== 0){ // Is there any command parameter?
|
||||||
|
if(strpos($match[3][$index], ",") !== -1){ // Is there multiple arguments
|
||||||
|
foreach(explode(",", $match[3][$index]) as $param){
|
||||||
|
// Param here is in form argName=argproperty.
|
||||||
|
// Parsing it to put it into the $params
|
||||||
|
$parts = explode("=", $param);
|
||||||
|
$params[$parts[0]] = $parts[1];
|
||||||
|
}
|
||||||
|
} else { // There is one argument
|
||||||
|
$parts = explode("=", $match[3][$index]);
|
||||||
|
$params[$parts[0]] = $parts[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $params;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build the regexr for parsing selectors in commands
|
||||||
|
* $1 is the selector character(s)
|
||||||
|
* $2 is "Is there any arguments to the command?"
|
||||||
|
* $3 is the list of arguments
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function buildRegExr(): string {
|
||||||
|
$regexr = "/ @("; // Space is to check that it's an argument on it's own and not a part of one
|
||||||
|
// Adding the selectors
|
||||||
|
$regexr .= preg_replace("/(\\$|\\(|\\)|\\^|\\[|\\])/", "\\\\$1", // Always parse input we don't trust!
|
||||||
|
implode("|", array_keys(self::$selectors))
|
||||||
|
);
|
||||||
|
// Adding the arguments
|
||||||
|
$regexr .= ")(\\[(((\w+)?=(.)+(,)?){1,})\\])?";
|
||||||
|
// Closing the regexr
|
||||||
|
$regexr .= "( |$)/"; // Space is to check that it's an argument on it's own and not a part of one (cf twitter accounts would we used with @+some letters)
|
||||||
|
return $regexr;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers a selector
|
||||||
|
*
|
||||||
|
* @param Selector $sel
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public static function registerSelector(Selector $sel): void{
|
||||||
|
self::$selectors[$sel->getSelectorChar()] = $sel;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unregisters a selector
|
||||||
|
*
|
||||||
|
* @param string $selChar The selector character
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public static function unregisterSelector(string $selChar): void{
|
||||||
|
unset(self::$selectors[$selChar]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a selector
|
||||||
|
*
|
||||||
|
* @param string $selChar The selector character
|
||||||
|
* @return Selector
|
||||||
|
*/
|
||||||
|
public static function getSelector(string $selChar): Selector{
|
||||||
|
return self::$selectors[$selChar];
|
||||||
|
}
|
||||||
|
}
|
47
src/Ad5001/PlayerSelectors/selector/AllPlayers.php
Normal file
47
src/Ad5001/PlayerSelectors/selector/AllPlayers.php
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Ad5001\PlayerSelectors\selector;
|
||||||
|
|
||||||
|
use pocketmine\command\CommandSender;
|
||||||
|
use pocketmine\Server;
|
||||||
|
use pocketmine\Player;
|
||||||
|
use pocketmine\level\Position;
|
||||||
|
|
||||||
|
|
||||||
|
class AllPlayers extends Selector{
|
||||||
|
|
||||||
|
public function __construct(){
|
||||||
|
parent::__construct("All players", "a", true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Executes the selector.
|
||||||
|
* Documentation is in the Selector.php file.
|
||||||
|
*
|
||||||
|
* @param CommandSender $sender
|
||||||
|
* @param array $parameters
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function applySelector(CommandSender $sender, array $parameters = []): array{
|
||||||
|
$defaultParams = Selector::DEFAULT_PARAMS;
|
||||||
|
if($sender instanceof Position){
|
||||||
|
$defaultParams["x"] = $sender->x;
|
||||||
|
$defaultParams["y"] = $sender->y;
|
||||||
|
$defaultParams["z"] = $sender->z;
|
||||||
|
}
|
||||||
|
$params = $parameters + $defaultParams;
|
||||||
|
$return = [];
|
||||||
|
foreach(Server::getInstance()->getOnlinePlayers() as $p){
|
||||||
|
if($params["c"] !== 0 && count($return) == $params["c"]) break; // Too much players
|
||||||
|
if($p->getLevel()->getName() !== $params["lvl"] && $params["lvl"] !== "") break; // Not in the right level
|
||||||
|
if(!$this->checkDefaultParams($p, $params)) {
|
||||||
|
echo "Not checked param";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
$return[] = $p->getName();
|
||||||
|
}
|
||||||
|
return $return;
|
||||||
|
}
|
||||||
|
}
|
50
src/Ad5001/PlayerSelectors/selector/ClosestPlayer.php
Normal file
50
src/Ad5001/PlayerSelectors/selector/ClosestPlayer.php
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Ad5001\PlayerSelectors\selector;
|
||||||
|
|
||||||
|
use pocketmine\command\CommandSender;
|
||||||
|
use pocketmine\Server;
|
||||||
|
use pocketmine\Player;
|
||||||
|
|
||||||
|
class ClosestPlayer extends Selector{
|
||||||
|
|
||||||
|
public function __construct(){
|
||||||
|
parent::__construct("Closest player", "p", false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Executes the selector.
|
||||||
|
* Documentation is in the Selector.php file.
|
||||||
|
*
|
||||||
|
* @param CommandSender $sender
|
||||||
|
* @param array $parameters
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function applySelector(CommandSender $sender, array $parameters = []): array{
|
||||||
|
$online = Server::getInstance()->getOnlinePlayers();
|
||||||
|
// Console
|
||||||
|
if(!($sender instanceof Player)) {
|
||||||
|
if(count($online) > 0){
|
||||||
|
return [$online[0]];
|
||||||
|
} else {
|
||||||
|
return [$sender->getName()];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Player
|
||||||
|
if(count($online) > 1){
|
||||||
|
// Checking the closest player
|
||||||
|
foreach($online as $p){
|
||||||
|
if($p->getLevel()->getName() == $sender->getLevel()->getName() &&
|
||||||
|
(!isset($selectedP) || $p->distanceSquared($sender) < $selectorP->distanceSquared($sender))){
|
||||||
|
$selectedP = $p;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return [$selectorP->getName()];
|
||||||
|
} else {
|
||||||
|
// Otherwise, just return sender's name because there's no other player.
|
||||||
|
return [$sender->getName()];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
48
src/Ad5001/PlayerSelectors/selector/Entities.php
Normal file
48
src/Ad5001/PlayerSelectors/selector/Entities.php
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
|
||||||
|
namespace Ad5001\PlayerSelectors\selector;
|
||||||
|
|
||||||
|
use pocketmine\command\CommandSender;
|
||||||
|
use pocketmine\Server;
|
||||||
|
use pocketmine\Player;
|
||||||
|
use pocketmine\level\Position;
|
||||||
|
|
||||||
|
use Ad5001\PlayerSelectors\Main;
|
||||||
|
|
||||||
|
|
||||||
|
class Entities extends Selector{
|
||||||
|
|
||||||
|
public function __construct(){
|
||||||
|
parent::__construct("Entities", "e", true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Executes the selector.
|
||||||
|
* Documentation is in the Selector.php file.
|
||||||
|
*
|
||||||
|
* @param CommandSender $sender
|
||||||
|
* @param array $parameters
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function applySelector(CommandSender $sender, array $parameters = []): array{
|
||||||
|
$defaultParams = Selector::DEFAULT_PARAMS;
|
||||||
|
if($sender instanceof Position){
|
||||||
|
$defaultParams["x"] = $sender->x;
|
||||||
|
$defaultParams["y"] = $sender->y;
|
||||||
|
$defaultParams["z"] = $sender->z;
|
||||||
|
}
|
||||||
|
$params = $parameters + $defaultParams;
|
||||||
|
$return = [];
|
||||||
|
foreach(Server::getInstance()->getLevels() as $lvl){
|
||||||
|
foreach($lvl->getEntities() as $e){
|
||||||
|
if($params["c"] !== 0 && count($return) == $params["c"]) break; // Too much players
|
||||||
|
if($e->getLevel()->getName() !== $params["lvl"] && $params["lvl"] !== "") break; // Not in the right level
|
||||||
|
if(!$this->checkDefaultParams($e, $params)) break;
|
||||||
|
$return[] = "e" . $e->getId();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return array_merge($return, Main::getSelector("a")->applySelector($sender, $parameters)); // Merging w/ all players so that it also adds players.
|
||||||
|
}
|
||||||
|
}
|
44
src/Ad5001/PlayerSelectors/selector/RandomPlayer.php
Normal file
44
src/Ad5001/PlayerSelectors/selector/RandomPlayer.php
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
|
||||||
|
namespace Ad5001\PlayerSelectors\selector;
|
||||||
|
|
||||||
|
use pocketmine\command\CommandSender;
|
||||||
|
use pocketmine\Server;
|
||||||
|
use pocketmine\Player;
|
||||||
|
use pocketmine\level\Position;
|
||||||
|
|
||||||
|
|
||||||
|
class RandomPlayer extends Selector{
|
||||||
|
|
||||||
|
public function __construct(){
|
||||||
|
parent::__construct("Random player", "r", true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Executes the selector.
|
||||||
|
* Documentation is in the Selector.php file.
|
||||||
|
*
|
||||||
|
* @param CommandSender $sender
|
||||||
|
* @param array $parameters
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function applySelector(CommandSender $sender, array $parameters = []): array{
|
||||||
|
$defaultParams = Selector::DEFAULT_PARAMS;
|
||||||
|
if($sender instanceof Position){
|
||||||
|
$defaultParams["x"] = $sender->x;
|
||||||
|
$defaultParams["y"] = $sender->y;
|
||||||
|
$defaultParams["z"] = $sender->z;
|
||||||
|
}
|
||||||
|
$params = $parameters + $defaultParams;
|
||||||
|
$possible = [];
|
||||||
|
foreach(Server::getInstance()->getOnlinePlayers() as $p){
|
||||||
|
if($p->getLevel()->getName() !== $params["lvl"] && $params["lvl"] !== "") break; // Not in the right level
|
||||||
|
if(!$this->checkDefaultParams($p, $params)) break;
|
||||||
|
$possible[] = $p;
|
||||||
|
}
|
||||||
|
if(count($possible) == 0) return [];
|
||||||
|
return [$possible[array_rand($possible)]->getName()];
|
||||||
|
}
|
||||||
|
}
|
123
src/Ad5001/PlayerSelectors/selector/Selector.php
Normal file
123
src/Ad5001/PlayerSelectors/selector/Selector.php
Normal file
|
@ -0,0 +1,123 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Ad5001\PlayerSelectors\selector;
|
||||||
|
|
||||||
|
use pocketmine\command\CommandSender;
|
||||||
|
use pocketmine\entity\Entity;
|
||||||
|
use pocketmine\Player;
|
||||||
|
use pocketmine\math\Vector3;
|
||||||
|
|
||||||
|
abstract class Selector {
|
||||||
|
/**
|
||||||
|
* Default minecraft selectors params.
|
||||||
|
* Can be used in selectors that supports them.
|
||||||
|
*/
|
||||||
|
const DEFAULT_PARAMS = [
|
||||||
|
// Coordinates of the center, will be modified if player in command
|
||||||
|
"x" => 0,
|
||||||
|
"y" => 0,
|
||||||
|
"z" => 0,
|
||||||
|
"lvl" => "", // The world to search in. Defaults to the current world in case of console, current world in the case of a player
|
||||||
|
// Search by coordinates
|
||||||
|
"dx" => 0, // Distance from the center in x (0 = no limit)
|
||||||
|
"dy" => 0, // Distance from the center in y (0 = no limit)
|
||||||
|
"dz" => 0, // Distance from the center in z (0 = no limit)
|
||||||
|
"r" => 0, // Radius max (0 = no limit)
|
||||||
|
"rm" => 0, // Radius min
|
||||||
|
// Search by pitch and yaw
|
||||||
|
"rx" => 180, // Pitch max
|
||||||
|
"rxm" => 0, // Pitch min
|
||||||
|
"ry" => 360, // Yaw max
|
||||||
|
"rym" => 0, // Yaw min
|
||||||
|
// Count searching
|
||||||
|
"c" => 0,
|
||||||
|
// Player parameters searching
|
||||||
|
"m" => -1, // Gamemode
|
||||||
|
"l" => PHP_INT_MAX, // Maximum xp level
|
||||||
|
"lm" => 0, // Min xp level
|
||||||
|
"name" => "", // Displayed name of the entity (display name for the player, name tag rename for entity)
|
||||||
|
"type" => "all", // Type of the entity (only for entity selector). "all" means all entities
|
||||||
|
"tag" => "Health", // Check if the tag exists in the entity/player.
|
||||||
|
];
|
||||||
|
|
||||||
|
protected $name;
|
||||||
|
protected $selectorChar;
|
||||||
|
protected $acceptModifiers;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Defines base for a selector
|
||||||
|
*
|
||||||
|
* @param string $name The name of the selector. E.g: "All players", "Entities", ...
|
||||||
|
* @param string $selectorChar What should be after the "@" to use this selector
|
||||||
|
* @param bool $acceptModifiers Should the selector accept modifiers? If false, arguments used in commands will be ignored. Can save alot of performances.
|
||||||
|
*/
|
||||||
|
public function __construct(string $name, string $selectorChar, bool $acceptModifiers){
|
||||||
|
$this->name = $name;
|
||||||
|
$this->selectorChar = $selectorChar;
|
||||||
|
$this->acceptModifiers = $acceptModifiers;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the name of the selector. E.g: "All players", "Entities", ...
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getName(): string{
|
||||||
|
return $this->name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the character of the selector. What should be after the "@" to use this selector
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getSelectorChar(): string{
|
||||||
|
return $this->selectorChar;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Should the selector accept modifiers? If false, arguments used in commands will be ignored. Can save alot of performances.
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function acceptsModifiers(): bool{
|
||||||
|
return $this->acceptModifiers;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets all the possible values for a selector depending on the sender and the parameters.
|
||||||
|
* Return should be an array of all the possible strings that match up to this selector.
|
||||||
|
*
|
||||||
|
* @param CommandSender $sender
|
||||||
|
* @param String[] $parameters
|
||||||
|
* @return String[]
|
||||||
|
*/
|
||||||
|
abstract public function applySelector(CommandSender $sender, array $parameters = []): array;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if an entity $et matches default params provided in $params.
|
||||||
|
*
|
||||||
|
* @param Entity $et
|
||||||
|
* @param array $params
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function checkDefaultParams(Entity $et, array $params): bool{
|
||||||
|
$dist = sqrt($et->distanceSquared(new Vector3($params["x"], $params["y"], $params["z"])));
|
||||||
|
if(($params["r"] !== 0 && $dist > $params["r"]) || $dist < $params["rm"]) return false; // Not in range
|
||||||
|
if($params["dx"] !== 0 && abs($et->x - $params["x"]) > $params["dx"]); // Not in x range
|
||||||
|
if($params["dy"] !== 0 && abs($et->y - $params["y"]) > $params["dy"]); // Not in y range
|
||||||
|
if($params["dz"] !== 0 && abs($et->z - $params["z"]) > $params["dz"]); // Not in z range
|
||||||
|
if($params["m"] !== -1 && $et instanceof Player && $et->getGamemode() !== $params["m"]) return false; // Not in the right mode.
|
||||||
|
if($params["rx"] < $et->getPitch() || $et->getPitch() < $params["rxm"]) return false; // Not in range pitch
|
||||||
|
if($params["ry"] < $et->getYaw() || $et->getYaw() < $params["rym"]) return false; // Not in range yaw
|
||||||
|
if($et instanceof Player && ($et->getXpLevel() > $params["l"] || $et->getXpLevel() < $params["lm"])) return false; // Not in range XP
|
||||||
|
if($params["name"] !== "" && (($et instanceof Player && $et->getDisplayName() !== $params["name"]) ||
|
||||||
|
(!($et instanceof Player) && $et->getNameTag() !== $params["name"]))) return false; // Not selected name
|
||||||
|
// Entity type
|
||||||
|
$etClassName = explode("\\", get_class($et))[count(explode("\\", get_class($et))) - 1];
|
||||||
|
if(substr($params["type"], 0, 1) == "!" && $etClassName == substr($params["type"], 1)) return false; // Should not be this kind of entity
|
||||||
|
if($params["type"] !== "all" && $etClassName !== $params["type"]) return false; // Not the required kind of entity
|
||||||
|
return true; // All checks passed!
|
||||||
|
}
|
||||||
|
}
|
28
src/Ad5001/PlayerSelectors/selector/SelfSelector.php
Normal file
28
src/Ad5001/PlayerSelectors/selector/SelfSelector.php
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Ad5001\PlayerSelectors\selector;
|
||||||
|
|
||||||
|
use pocketmine\command\CommandSender;
|
||||||
|
use pocketmine\Server;
|
||||||
|
use pocketmine\Player;
|
||||||
|
|
||||||
|
class SelfSelector extends Selector{
|
||||||
|
|
||||||
|
public function __construct(){
|
||||||
|
parent::__construct("Self", "s", false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Executes the selector.
|
||||||
|
* Documentation is in the Selector.php file.
|
||||||
|
*
|
||||||
|
* @param CommandSender $sender
|
||||||
|
* @param array $parameters
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function applySelector(CommandSender $sender, array $parameters = []): array{
|
||||||
|
return [$sender->getName()];
|
||||||
|
}
|
||||||
|
}
|
48
src/Ad5001/PlayerSelectors/selector/WorldPlayers.php
Normal file
48
src/Ad5001/PlayerSelectors/selector/WorldPlayers.php
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
|
||||||
|
namespace Ad5001\PlayerSelectors\selector;
|
||||||
|
|
||||||
|
use pocketmine\command\CommandSender;
|
||||||
|
use pocketmine\Server;
|
||||||
|
use pocketmine\Player;
|
||||||
|
use pocketmine\level\Position;
|
||||||
|
|
||||||
|
|
||||||
|
class WorldPlayers extends Selector{
|
||||||
|
|
||||||
|
public function __construct(){
|
||||||
|
parent::__construct("World players", "w", true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Executes the selector.
|
||||||
|
* Documentation is in the Selector.php file.
|
||||||
|
*
|
||||||
|
* @param CommandSender $sender
|
||||||
|
* @param array $parameters
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function applySelector(CommandSender $sender, array $parameters = []): array{
|
||||||
|
$defaultParams = Selector::DEFAULT_PARAMS;
|
||||||
|
if($sender instanceof Position){
|
||||||
|
$defaultParams["x"] = $sender->x;
|
||||||
|
$defaultParams["y"] = $sender->y;
|
||||||
|
$defaultParams["z"] = $sender->z;
|
||||||
|
$defaultParams["lvl"] = $sender->getLevel()->getName();
|
||||||
|
} else {
|
||||||
|
$defaultParams["lvl"] = Server::getInstance()->getDefaultLevel()->getName();
|
||||||
|
|
||||||
|
}
|
||||||
|
$params = $parameters + $defaultParams;
|
||||||
|
$return = [];
|
||||||
|
foreach(Server::getInstance()->getOnlinePlayers() as $p){
|
||||||
|
if($params["c"] !== 0 && count($return) == $params["c"]) break; // Too much players
|
||||||
|
if($p->getLevel()->getName() !== $params["lvl"] && $params["lvl"] !== "") break; // Not in the right level
|
||||||
|
if(!$this->checkDefaultParams($p, $params)) break;
|
||||||
|
$return[] = $p->getName();
|
||||||
|
}
|
||||||
|
return $return;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue