Skip to content
This repository has been archived by the owner on Jun 12, 2018. It is now read-only.

Commit

Permalink
Fix crash with resetpassword on mysql + JSON support
Browse files Browse the repository at this point in the history
  • Loading branch information
DaPigGuy committed Apr 19, 2017
1 parent 9fa49eb commit 650c1f3
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 35 deletions.
2 changes: 1 addition & 1 deletion plugin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: PiggyAuth
main: PiggyAuth\Main
version: 3.0.0.14
version: 3.0.0.15
api: [2.0.0, 3.0.0-ALPHA1, 3.0.0-ALPHA2, 3.0.0-ALPHA3, 3.0.0-ALPHA4, 3.0.0-APLHA5]
load: POSTWORLD
author: MCPEPIG
Expand Down
2 changes: 1 addition & 1 deletion resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ key:
enabled: true
#Username of owner, for key
owner: TheOwnerLovesPig
#Database (sqlite3, mysql, yaml)
#Database (sqlite3, mysql, yaml, json)
database: sqlite3
#MySQL login
mysql:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,17 @@
use pocketmine\Player;
use pocketmine\utils\Config;

/**
* Class YAML
* @package PiggyAuth\Databases
*/
class YAML implements Database

class IndividualFiles implements Database
{
private $plugin;
private $extension;

/**
* YAML constructor.
* @param Main $plugin
*/
public function __construct(Main $plugin)

public function __construct(Main $plugin, $extension)
{
$this->plugin = $plugin;
$this->extension = $extension;
@mkdir($this->plugin->getDataFolder() . "players");
}

Expand All @@ -32,8 +28,8 @@ public function getRegisteredCount()
{
$files = scandir($this->plugin->getDataFolder() . "players/");
$count = 0;
foreach($files as $file){
if(strpos($file, ".yml") !== false){
foreach ($files as $file) {
if (strpos($file, "." . $this->extension) !== false) {
$count++;
}
}
Expand All @@ -47,10 +43,10 @@ public function getRegisteredCount()
*/
public function getPlayer($player, $callback, $args)
{
if(file_exists($this->plugin->getDataFolder() . "players/" . strtolower($player) . ".yml")){
$file = new Config($this->plugin->getDataFolder() . "players/" . strtolower($player) . ".yml");
if (file_exists($this->plugin->getDataFolder() . "players/" . strtolower($player) . "." . $this->extension)) {
$file = new Config($this->plugin->getDataFolder() . "players/" . strtolower($player) . "." . $this->extension);
$data = $file->getAll();
}else{
} else {
$data = null;
}
if ($callback !== null) {
Expand All @@ -63,8 +59,8 @@ public function getPlayer($player, $callback, $args)
*/
public function getOfflinePlayer($player)
{
if(file_exists($this->plugin->getDataFolder() . "players/" . strtolower($player) . ".yml")){
$file = new Config($this->plugin->getDataFolder() . "players/" . strtolower($player) . ".yml");
if (file_exists($this->plugin->getDataFolder() . "players/" . strtolower($player) . "." . $this->extension)) {
$file = new Config($this->plugin->getDataFolder() . "players/" . strtolower($player) . "." . $this->extension);
return $file->getAll();
}
return null;
Expand All @@ -80,7 +76,7 @@ public function getOfflinePlayer($player)
*/
public function updatePlayer($player, $column, $arg, $type = 0, $callback = null, $args = null)
{
$file = new Config($this->plugin->getDataFolder() . "players/" . strtolower($player) . ".yml");
$file = new Config($this->plugin->getDataFolder() . "players/" . strtolower($player) . "." . $this->extension);
$file->set($column, $arg);
$file->save();
if ($callback !== null) {
Expand All @@ -99,7 +95,16 @@ public function updatePlayer($player, $column, $arg, $type = 0, $callback = null
*/
public function insertData(Player $player, $password, $email, $pin, $xbox, $callback = null, $args = null)
{
$file = new Config($this->plugin->getDataFolder() . "players/" . strtolower($player->getName()) . ".yml", Config::YAML, [
$type = Config::YAML;
switch ($this->extension) {
case "yml":
$type = Config::YAML;
break;
case "json":
$type = Config::JSON;
break;
}
$file = new Config($this->plugin->getDataFolder() . "players/" . strtolower($player->getName()) . "." . $this->extension, $type, [
"password" => $password,
"email" => $email,
"pin" => $pin,
Expand All @@ -125,7 +130,16 @@ public function insertData(Player $player, $password, $email, $pin, $xbox, $call
*/
public function insertDataWithoutPlayerObject($player, $password, $email, $pin, $auth = "PiggyAuth", $callback = null, $args = null)
{
$file = new Config($this->plugin->getDataFolder() . "players/" . strtolower($player) . ".yml", Config::YAML, [
$type = Config::YAML;
switch ($this->extension) {
case "yml":
$type = Config::YAML;
break;
case "json":
$type = Config::JSON;
break;
}
$file = new Config($this->plugin->getDataFolder() . "players/" . strtolower($player) . "." . $this->extension, $type, [
"password" => $password,
"email" => $email,
"pin" => $pin,
Expand All @@ -147,7 +161,7 @@ public function insertDataWithoutPlayerObject($player, $password, $email, $pin,
*/
public function clearPassword($player, $callback = null, $args = null)
{
$result = @unlink($this->plugin->getDataFolder() . "players/" . $player . ".yml");
$result = @unlink($this->plugin->getDataFolder() . "players/" . $player . "." . $this->extension);
if ($callback !== null) {
$callback($result, $args, $this->plugin);
}
Expand Down
4 changes: 1 addition & 3 deletions src/PiggyAuth/EventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
namespace PiggyAuth;

use PiggyAuth\Databases\MySQL;
use PiggyAuth\Databases\SQLite3;
use PiggyAuth\Databases\YAML;
use PiggyAuth\Events\PlayerFailEvent;
use PiggyAuth\Events\PlayerLoginEvent;

Expand Down Expand Up @@ -296,7 +294,7 @@ public function onConsume(PlayerItemConsumeEvent $event)
public function onJoin(PlayerJoinEvent $event)
{
$player = $event->getPlayer();
if ($this->plugin->database instanceof SQLite3 || $this->plugin->database instanceof YAML) {
if (!$this->plugin->database instanceof MySQL) {
$this->plugin->sessionmanager->loadSession($player);
}
$data = $this->plugin->sessionmanager->getSession($player)->getData();
Expand Down
25 changes: 16 additions & 9 deletions src/PiggyAuth/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use PiggyAuth\Commands\UnregisterCommand;
use PiggyAuth\Converter\ServerAuthConverter;
use PiggyAuth\Converter\SimpleAuthConverter;
use PiggyAuth\Databases\YAML;
use PiggyAuth\Databases\IndividualFiles;
use PiggyAuth\Emails\EmailManager;
use PiggyAuth\Events\PlayerChangePasswordEvent;
use PiggyAuth\Events\PlayerFailEvent;
Expand Down Expand Up @@ -148,7 +148,10 @@ public function onEnable()
break;
case "yml":
case "yaml":
$this->database = new YAML($this);
$this->database = new IndividualFiles($this, "yml");
break;
case "json":
$this->database = new IndividualFiles($this, "json");
break;
default:
$this->database = new SQLite3($this);
Expand All @@ -171,7 +174,7 @@ public function onEnable()
}

/**
* @return MySQL|SQLite3|YAML
* @return MySQL|SQLite3|IndividualFiles
*/
public function getDatabase()
{
Expand Down Expand Up @@ -443,14 +446,16 @@ public function force(Player $player, $login = true, $mode = 0, $rehashedpasswor
$player->getInventory()->sendContents($player);
}
if ($this->getConfig()->getNested("login.adventure-mode")) {
if($this->sessionmanager->getSession($player)->getGamemode() !== null) {
if ($this->sessionmanager->getSession($player)->getGamemode() !== null) {
$player->setGamemode($this->sessionmanager->getSession($player)->getGamemode());
$this->sessionmanager->getSession($player)->setGamemode(null);
}
}
if ($this->getConfig()->getNested("message.boss-bar")) {
$this->sessionmanager->getSession($player)->getWither()->kill();
$this->sessionmanager->getSession($player)->setWither(null);
if ($this->sessionmanager->getSession($player)->getWither() !== null) {
$this->sessionmanager->getSession($player)->getWither()->kill();
$this->sessionmanager->getSession($player)->setWither(null);
}
}
if ($rehashedpassword !== null) {
$this->sessionmanager->getSession($player)->updatePlayer("password", $rehashedpassword);
Expand Down Expand Up @@ -780,7 +785,7 @@ public function logout(Player $player, $quit = true)
if ($this->sessionmanager->getSession($player) !== null && $this->sessionmanager->getSession($player)->isAuthenticated()) {
$this->sessionmanager->getSession($player)->setAuthenticated(false);
} else {
if($this->sessionmanager->getSession($player) !== null) {
if ($this->sessionmanager->getSession($player) !== null) {
if ($this->getConfig()->getNested("login.adventure-mode")) {
if ($this->sessionmanager->getSession($player)->getGamemode() !== null) {
$player->setGamemode($this->sessionmanager->getSession($player)->getGamemode());
Expand All @@ -791,8 +796,10 @@ public function logout(Player $player, $quit = true)
$this->sessionmanager->getSession($player)->setTimeoutTick(0);
$this->sessionmanager->getSession($player)->setTries(0);
if ($this->getConfig()->getNested("message.boss-bar")) {
$this->sessionmanager->getSession($player)->getWither()->kill();
$this->sessionmanager->getSession($player)->setWither(null);
if ($this->sessionmanager->getSession($player)->getWither() !== null) {
$this->sessionmanager->getSession($player)->getWither()->kill();
$this->sessionmanager->getSession($player)->setWither(null);
}
}
}
}
Expand Down

0 comments on commit 650c1f3

Please sign in to comment.