forked from PocketDock/PocketDockConsole
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.php
More file actions
118 lines (104 loc) · 4.58 KB
/
Main.php
File metadata and controls
118 lines (104 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
namespace PocketDockConsole;
use pocketmine\plugin\PluginBase;
use pocketmine\utils\TextFormat;
use pocketmine\command\Command;
use pocketmine\command\CommandSender;
use pocketmine\event\Listener;
use pocketmine\event\player\PlayerLoginEvent;
use pocketmine\event\player\PlayerQuitEvent;
use pocketmine\event\player\PlayerRespawnEvent;
use pocketmine\ThreadManager;
class Main extends PluginBase implements Listener {
public $attachment = null;
public function onLoad() {
$this->getLogger()->info(TextFormat::WHITE . "Loaded");
}
public function onEnable() {
$this->getServer()->getPluginManager()->registerEvents($this, $this);
$this->saveDefaultConfig();
$this->reloadConfig();
$this->getLogger()->info(TextFormat::DARK_GREEN . "Enabled");
$this->setPassword();
$this->legacy = true;
if (method_exists($this->getServer(), "getNetwork")) {
$this->legacy = false;
}
$this->thread = new PDCServer("0.0.0.0", $this->getConfig()->get("port"), $this->getServer()->getLogger(), $this->getServer()->getLoader(), $this->getConfig()->get("password"), stream_get_contents($pluginIndex = $this->getResource("PluginIndex.html")), $this->getConfig()->get("backlog"), $this->legacy);
@fclose($pluginIndex);
$this->rc = new RunCommand($this);
$this->getScheduler()->scheduleRepeatingTask($this->rc, 1);
$this->lastBufferLine = "";
$this->attachment = new Attachment($this->thread);
$this->getServer()->getLogger()->addAttachment($this->attachment);
}
public function setPassword() {
if ($this->getConfig()->get("password") == "PocketDockRules!") {
$this->getConfig()->set("password", $this->getServer()->getConfigString("rcon.password", ""));
$this->getLogger()->info("The password is now the RCON password.");
$this->getLogger()->info("If you would like to change the password, please do so in the PDC config.");
$this->getConfig()->save();
$this->reloadConfig();
}
}
public function onCommand(CommandSender $sender, Command $command, string $label, array $args): bool {
switch ($command->getName()) {
case "consoleclients":
if (!$sender->hasPermission("pocketdockconsole.command.consoleclients")) {
$sender->sendMessage(TextFormat::RED . "[PDC] Get some permissions...");
return true;
}
$authedclients = explode(";", $this->thread->connectedips);
if (count($authedclients) < 2) {
$sender->sendMessage("[PDC] There are no connected clients");
return true;
}
$sender->sendMessage("[PDC] Connected client(s) are: " . implode("; ", $authedclients));
return true;
case "killclient":
if (!$sender->hasPermission("pocketdockconsole.command.killclient")) {
$sender->sendMessage(TextFormat::RED . "[PDC] Get some permissions...");
return true;
}
if (!isset($args[0])) {
$sender->sendMessage($command->getUsage());
return true;
}
$sender->sendMessage("[PDC] Killing client: " . $args[0]);
$this->thread->clienttokill = $args[0];
return true;
default:
return false;
}
}
public function PlayerLoginEvent(PlayerLoginEvent $event) {
$this->rc->updateInfo();
$this->sendFiles();
}
public function PlayerQuitEvent(PlayerQuitEvent $event) {
$name = $event->getPlayer()->getName();
$this->rc->updateInfo($name);
}
public function PlayerRespawnEvent(PlayerRespawnEvent $event) {
$this->rc->updateInfo();
}
public function getFiles($dir) {
$objects = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir));
foreach ($objects as $name => $object) {
if (!strpos($name, "bin")) {
$names[] = $name;
}
}
return $names;
}
public function sendFiles() {
if ($this->getConfig()->get("viewfiles")) {
$this->thread->jsonStream.= json_encode(array("type" => "files", "files" => $this->getFiles(realpath($this->getServer()->getDataPath())))) . "\n";
}
return false;
}
public function onDisable() {
$this->getLogger()->info(TextFormat::DARK_RED . "Disabled");
$this->thread->stop();
}
}