Created
December 17, 2016 16:20
-
-
Save robske110/2531b8a6f1c4baa417cbb3a36ddef9d3 to your computer and use it in GitHub Desktop.
A small (Script)Plugin that does all kind of crazy stuff to the player...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @name LayDown | |
* @main robske_110\LayDown\LayDown | |
* @version 1.0.0alpha | |
* @api 2.1.0 | |
* @description Experience everything Upside Down! | |
* @author robske_110 | |
*/ | |
namespace robske_110\LayDown{ | |
use robske_110\SPC\ScriptPluginCommands; | |
use robske_110\Utils\Utils; | |
use pocketmine\event\Listener; | |
use pocketmine\plugin\PluginBase; | |
use pocketmine\Player; | |
use pocketmine\command\Command; | |
use pocketmine\command\CommandSender; | |
class LayDown extends PluginBase{ | |
public function onLoad(){ | |
$id = ScriptPluginCommands::initFor($this); | |
ScriptPluginCommands::addCommand($id, [ | |
'name' => 'laydown', | |
'description' => 'Makes the player behave like he is sleeping.', | |
'permission' => 'op', | |
'usage' => '/laydown or /laydown <player>' | |
]); | |
ScriptPluginCommands::addCommand($id, [ | |
'name' => 'layup', | |
'description' => 'Makes the player behave normally again.', | |
'permission' => 'op', | |
'usage' => '/layup or /layup <player>' | |
]); | |
$this->getServer()->getCommandMap()->registerAll($this->getDescription()->getName(), ScriptPluginCommands::getCommands($id)); | |
} | |
public function onEnable(){ | |
Utils::init($this, false); | |
} | |
public function onDisable(){ | |
Utils::close(); | |
} | |
public function onCommand(CommandSender $sender, Command $command, $label, array $args){ | |
switch($command->getName()){ | |
case "laydown": | |
if(isset($args[0])){ | |
if(($player = $this->getServer()->getPlayer($args[0])) instanceof Player){ | |
$player->setDataFlag(Player::DATA_PLAYER_FLAGS, Player::DATA_PLAYER_FLAG_SLEEP, true, Player::DATA_TYPE_BYTE); | |
$player->setDataProperty(Player::DATA_PLAYER_BED_POSITION, Player::DATA_TYPE_POS, [$player->x,$player->y,$player->z], false); | |
return true; | |
} | |
Utils::sendMsgToSender($sender, "Could not find player '".$args[0]."'!"); | |
return false; | |
} | |
if(Utils::getTypeAsNameOfSender($sender) == "CONSOLE"){ | |
Utils::sendMsgToSender($sender, "You need to specify the player from the console!"); | |
return false; | |
} | |
$sender->setDataFlag(Player::DATA_PLAYER_FLAGS, Player::DATA_PLAYER_FLAG_SLEEP, true, Player::DATA_TYPE_BYTE); | |
$sender->setDataProperty(Player::DATA_PLAYER_BED_POSITION, Player::DATA_TYPE_POS, [$sender->x,$sender->y,$sender->z], false); | |
break; | |
case "layup": | |
if(isset($args[0])){ | |
if(($player = $this->getServer()->getPlayer($args[0])) instanceof Player){ | |
$player->setDataFlag(Player::DATA_PLAYER_FLAGS, Player::DATA_PLAYER_FLAG_SLEEP, false, Player::DATA_TYPE_BYTE); | |
#$player->setDataProperty(Player::DATA_PLAYER_BED_POSITION, Player::DATA_TYPE_POS, [$player->x,$player->y,$player->z], false); | |
return true; | |
} | |
Utils::sendMsgToSender($sender, "Could not find player '".$args[0]."'!"); | |
return false; | |
} | |
if(Utils::getTypeAsNameOfSender($sender) == "CONSOLE"){ | |
Utils::sendMsgToSender($sender, "You need to specify the player from the console!"); | |
return false; | |
} | |
$sender->setDataFlag(Player::DATA_PLAYER_FLAGS, Player::DATA_PLAYER_FLAG_SLEEP, false, Player::DATA_TYPE_BYTE); | |
#$sender->setDataProperty(Player::DATA_PLAYER_BED_POSITION, Player::DATA_TYPE_POS, [$sender->x,$sender->y,$sender->z], false); | |
break; | |
} | |
} | |
} | |
} | |
/** LIBARIES */ | |
namespace robske_110\SPC{ | |
use pocketmine\command\PluginCommand; | |
use pocketmine\Plugin\Plugin; | |
use robske_110\Utils\Utils; | |
/** | |
* @author robske_110 | |
* @version 0.1.2-php7 | |
*/ | |
abstract class ScriptPluginCommands{ | |
private static $plugins; | |
public static function initFor(Plugin $plugin) : int { | |
self::$plugins[] = [$plugin]; | |
return count(self::$plugins) - 1; | |
} | |
public static function addCommand(int $id, array $data){ | |
if(!isset(self::$plugins[$id])){ | |
Utils::critical("addCommand() has been called with an unkown ID!"); | |
} | |
$cmd = new PluginCommand($data["name"], self::$plugins[$id][0]); | |
if(isset($data["description"])){ | |
$cmd->setDescription($data["description"]); | |
} | |
if(isset($data["usage"])){ | |
$cmd->setUsage($data["usage"]); | |
} | |
if(isset($data["permission"])){ | |
$cmd->setPermission($data["permission"]); | |
} | |
if(isset($data["aliases"]) && is_array($data["aliases"])){ | |
$aliases = []; | |
foreach($data["aliases"] as $alias){ | |
$aliases[] = $alias; | |
} | |
$cmd->setAliases($aliases); | |
} | |
self::$plugins[$id][1][] = $cmd; | |
} | |
public static function getCommands(int $id) : array { | |
if(!isset(self::$plugins[$id])){ | |
Utils::critical("getCommands() has been called with an unkown ID!"); | |
} | |
return self::$plugins[$id][1]; | |
} | |
} | |
} | |
namespace robske_110\Utils{ | |
use pocketmine\Server; | |
use pocketmine\Plugin\Plugin; | |
use pocketmine\Player; | |
/** | |
* @author robske_110 | |
* @version 1.0.0-InDev [Not yet updated to php7!] | |
*/ | |
abstract class Utils{ | |
private static $logger; | |
private static $debugEnabled; | |
private static $debugFile; | |
const LOG_LVL_INFO = 0; | |
const LOG_LVL_WARNING = 1; | |
const LOG_LVL_CRITICAL = 2; | |
const LOG_LVL_EMERGENCY = 3; | |
const LOG_LVL_DEBUG = 4; | |
const DEBUG_LVL_IMPORTED = 0; | |
const DEBUG_LVL_NORMAL = 1; | |
const DEBUG_LVL_PRIOR = 2; | |
public static function init(Plugin $main, $debugEnabled = false){ | |
self::$logger = $main->getLogger(); | |
self::$debugEnabled = $debugEnabled; | |
if($debugEnabled){ | |
$filename = $main->getDataFolder()."BanWarnDebug".date("d:m:Y_H-i-s", time()).".txt"; | |
self::$debugFile = fopen($filename,'w+'); | |
if(!self::$debugFile){ | |
self::$debugEnabled = false; | |
self::warning("Failed to create/open '".$filename."' for writing! Writing debug to file is disabled!"); | |
} | |
} | |
} | |
public static function close(){ | |
if(self::$debugEnabled){ | |
fclose(self::$debugFile); | |
} | |
} | |
public static function sendMsgToSender($sender, $msg){ | |
if($sender instanceof Player){ | |
$sender->getPlayer()->sendMessage(PLUGIN_MAIN_PREFIX.$message); | |
}else{ | |
self::log($msg); | |
} | |
} | |
public static function getTypeAsNameOfSender($sender){ | |
if($sender instanceof Player){ | |
$name = $sender->getPlayer()->getName(); | |
}else{ | |
$name = "CONSOLE"; | |
} | |
return $name; | |
} | |
public static function log($msg, $logLvl = self::LOG_LVL_INFO){ | |
switch($logLvl){ | |
case self::LOG_LVL_INFO: self::$logger->info($msg); break; | |
case self::LOG_LVL_WARNING: self::$logger->warning($msg); break; | |
case self::LOG_LVL_CRITICAL: self::$logger->critical($msg); break; | |
case self::LOG_LVL_EMERGENCY: self::$logger->emergency($msg); break; | |
case self::LOG_LVL_DEBUG: self::$logger->debug($msg); break; | |
} | |
} | |
public static function warning($msg){ | |
self::log($msg, self::LOG_LVL_WARNING); | |
self::debug($msg, self::DEBUG_LVL_IMPORTED); | |
} | |
public static function critical($msg){ | |
self::log($msg, self::LOG_LVL_CRITICAL); | |
self::debug($msg, self::DEBUG_LVL_IMPORTED); | |
} | |
public static function emergency($msg){ | |
self::log($msg, self::LOG_LVL_EMERGENCY); | |
self::debug($msg, self::DEBUG_LVL_IMPORTED); | |
} | |
public static function debug($msg, $debugLvl = self::DEBUG_LVL_NORMAL){ | |
if($debugLvl !== self::DEBUG_LVL_IMPORTED){ | |
self::log($msg, self::LOG_LVL_DEBUG); | |
} | |
if(self::$debugEnabled){ | |
switch($debugLvl){ | |
case self::DEBUG_LVL_IMPORTED: $msg = "[IMPORTED] ".$msg."<"; break; //Imported debug msgs are imported from logger critical, warning and emergency msgs. | |
case self::DEBUG_LVL_NORMAL: $msg = "[NORMAL] ".$msg."<"; break; | |
case self::DEBUG_LVL_PRIOR: $msg = "[PRIOR] !".$msg."!<"; break; | |
} | |
$msg .= "\n"; | |
fwrite(self::$debugFile, $msg); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment