Add saving/loading

Added player saving and loading!
This commit is contained in:
Matthew 2018-04-15 17:27:16 +02:00
parent df59a11663
commit c3ee373c27
4 changed files with 134 additions and 8 deletions

View File

@ -3,6 +3,9 @@
# True = enabled
# False = disabled
#Save playerdata
Save: true
# Show players name
Name: true

View File

@ -7,6 +7,7 @@ use pocketmine\command\Command;
use pocketmine\command\CommandSender;
use pocketmine\command\ConsoleCommandSender;
use pocketmine\event\Listener;
use pocketmine\event\player\PlayerJoinEvent;
use pocketmine\event\server\DataPacketReceiveEvent;
use pocketmine\network\mcpe\protocol\LoginPacket;
use pocketmine\Player;
@ -27,30 +28,52 @@ class PlayerInfo extends PluginBase implements Listener {
if(!is_dir($this->getDataFolder())) {
mkdir($this->getDataFolder());
}
if(!is_dir($this->getDataFolder() . "players")) {
mkdir($this->getDataFolder() . "players");
}
if(!file_exists($this->getDataFolder() . "config.yml")) {
$this->saveDefaultConfig();
}
if(!file_exists($this->getDataFolder() . "models.yml")) {
$this->saveResource("models.yml", false);
}
SpoonDetector::printSpoon($this, 'spoon.txt');
$this->getLogger()->notice("is enabled");
}
public function onDisable() {
$this->getLogger()->notice("is disabled!");
}
public function onPacketReceived(DataPacketReceiveEvent $receiveEvent) {
$pk = $receiveEvent->getPacket();
if($pk instanceof LoginPacket) {
$this->PlayerData[$pk->username] = $pk->clientData;
}
}
public function onJoin(PlayerJoinEvent $joinEvent) {
$player = $joinEvent->getPlayer();
$cdata = $this->PlayerData[$player->getName()];
$os = ["Unknown", "Android", "iOS", "macOS", "FireOS", "GearVR", "HoloLens", "Windows 10", "Windows", "Dedicated", "Orbis", "NX"];
$UI = ["Classic UI", "Pocket UI"];
$Controls = ["Unknown", "Mouse", "Touch", "Controller"];
$GUI = [-2 => "Minimum", -1 => "Medium", 0 => "Maximum"];
$this->getServer()->getScheduler()->scheduleTask(new Tasks\SaveTask(
$this,
$player->getName(),
$this->DeviceModel($cdata["DeviceModel"]),
$os[$cdata["DeviceOS"]],
$player->getAddress(),
$UI[$cdata["UIProfile"]],
$GUI[$cdata["GuiScale"]],
$Controls[$cdata["CurrentInputMode"]]
));
}
public function DeviceModel(string $model) {
$models = yaml_parse_file($this->getDataFolder() . "models.yml");
@ -74,23 +97,33 @@ class PlayerInfo extends PluginBase implements Listener {
if(isset($args[0])) {
$target = $this->getServer()->getPlayer($args[0]);
} else {
$sender->sendMessage(TF::RED . "[Error] Please specify a player");
$sender->sendMessage(TF::RED . "[PlayerInfo] Please specify a player");
return false;
}
} else {
if($sender instanceof Player and !isset($args[0])) {
$target = $sender->getPlayer();
} else {
$target = $this->getServer()->getPlayer($args[0]);
if($target = $this->getServer()->getPlayer($args[0])) {
//Nothing
} else {
if($this->getConfig()->get("IP") == true) {
$this->getServer()->getScheduler()->scheduleTask(new Tasks\LoadTask($this, $sender, $args[0]));
return true;
} else {
$sender->sendMessage(TF::RED . "[PlayerInfo] Player is not online");
return false;
}
}
}
}
} else {
$sender->sendMessage(TF::RED . "[Error] No permission");
$sender->sendMessage(TF::RED . "[PlayerInfo] No permission");
return false;
}
if($target instanceof Player) {
$cdata = $this->PlayerData[$target->getName()];
$sender->sendMessage(TF::GREEN . TF::BOLD . "===" . TF::GREEN . "Player Info" . TF::GREEN . TF::BOLD . "===");
$sender->sendMessage(TF::GREEN . TF::BOLD . "===" . TF::GREEN . "PlayerInfo" . TF::GREEN . TF::BOLD . "===");
if($this->getConfig()->get("Name") == true) {
$sender->sendMessage(TF::AQUA . "Name: " . TF::RED . $target->getDisplayName());
}
@ -124,7 +157,7 @@ class PlayerInfo extends PluginBase implements Listener {
$sender->sendMessage(TF::GREEN . TF::BOLD . "================");
return true;
} else {
$sender->sendMessage(TF::RED . "[Error] Player is not online");
$sender->sendMessage(TF::RED . "[PlayerInfo] Player is not online");
return false;
}
}

View File

@ -0,0 +1,43 @@
<?php
namespace Matthww\PlayerInfo\Tasks;
use pocketmine\command\CommandSender;
use pocketmine\scheduler\PluginTask;
use pocketmine\utils\Config;
use pocketmine\utils\TextFormat as TF;
class LoadTask extends PluginTask {
public $plugin;
protected $sender;
protected $target;
public function __construct($plugin, CommandSender $sender, string $target) {
$this->plugin = $plugin;
$this->sender = $sender;
$this->target = $target;
parent::__construct($plugin);
}
public function getPlugin() {
return $this->plugin;
}
public function onRun(int $tick) {
if(!file_exists($this->getPlugin()->getDataFolder() . "players/" . strtolower($this->target) . ".json")) {
$this->sender->sendMessage(TF::colorize("&c[PlayerInfo] Player &f". $this->target . " &cwas not found!"));
} else {
$data = new Config($this->getPlugin()->getDataFolder() . "players/" . strtolower($this->target) . ".json", Config::JSON);
$this->sender->sendMessage(TF::colorize("&a&l=== &r&aPlayerInfo &a&l==="));
$this->sender->sendMessage(TF::colorize("&bName: &c" . $data->get("Name")));
$this->sender->sendMessage(TF::colorize("&bIP: &c" . $data->get("IP")));
$this->sender->sendMessage(TF::colorize("&bOS: &c" . $data->get("OS")));
$this->sender->sendMessage(TF::colorize("&bModel: &c" . $data->get("Model")));
$this->sender->sendMessage(TF::colorize("&bUI: &c" . $data->get("UI")));
$this->sender->sendMessage(TF::colorize("&bGUI Scale: &c" . $data->get("GUI")));
$this->sender->sendMessage(TF::colorize("&bControls: &c" . $data->get("Controls")));
$this->sender->sendMessage(TF::colorize("&a&l================"));
}
}
}

View File

@ -0,0 +1,47 @@
<?php
namespace Matthww\PlayerInfo\Tasks;
use pocketmine\scheduler\PluginTask;
use pocketmine\utils\Config;
class SaveTask extends PluginTask {
public $plugin;
protected $player;
protected $model;
protected $os;
protected $ip;
protected $UI;
protected $GUI;
protected $controls;
public function __construct($plugin, string $player, string $model, string $os, string $ip, string $UI, string $GUI, string $controls) {
$this->plugin = $plugin;
$this->player = $player;
$this->model = $model;
$this->os = $os;
$this->ip = $ip;
$this->UI = $UI;
$this->GUI = $GUI;
$this->controls = $controls;
parent::__construct($plugin);
}
public function getPlugin() {
return $this->plugin;
}
public function onRun(int $tick) {
$data = new Config($this->getPlugin()->getDataFolder() . "players/" . strtolower($this->player) . ".json", Config::JSON);
$data->set("Name", $this->player);
$data->set("Model", $this->model);
$data->set("OS", $this->os);
$data->set("IP", $this->ip);
$data->set("UI", $this->UI);
$data->set("GUI", $this->GUI);
$data->set("Controls", $this->controls);
$data->save();
$data->reload();
}
}