Skip to content

Commit

Permalink
Merge pull request #6 from PocketDock/plugins
Browse files Browse the repository at this point in the history
Added a plugin manager and squashed bugs
  • Loading branch information
Humerus committed Dec 21, 2014
2 parents 54aa645 + 971319c commit 64d9bab
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 7 deletions.
2 changes: 1 addition & 1 deletion plugin.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: PocketDockConsole
prefix: PDC
main: PocketDockConsole\Main
version: 0.0.7
version: 0.0.8
api: [1.0.0]

load: POSTWORLD
Expand Down
150 changes: 145 additions & 5 deletions resources/PluginIndex.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
<title>PocketDockConsole</title>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/themes/smoothness/jquery-ui.css" />
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>
<script src="//cdn.rawgit.com/chjj/term.js/0b10f6c55d5113d50d0ff94b6c38a46375a5f9a5/src/term.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js" type="text/javascript"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/theme-monokai.js" type="text/javascript"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ext-modelist.js" type="text/javascript"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/datatables/1.10.0/js/jquery.dataTables.min.js"></script>
<script src="//cdn.datatables.net/fixedcolumns/3.0.1/js/dataTables.fixedColumns.min.js"></script>
<style>
body {
font-size: 15px;
Expand Down Expand Up @@ -132,10 +136,106 @@
width: 100%;
margin: auto;
}
#pluginsModalDialog {
width: 100%;
height: auto;
}

ul.ui-autocomplete {
z-index: 1100;
}

#plugin-description {
font-size: 0.9em
}
</style>
</head>

<script type="text/javascript">
function addRow(data) {
$('#pluginsTable > tbody:last').append('<tr><td><input type="checkbox" id="p' + data.title.replace(/ /g, "-") + '"name="id[]" value="' + data.id + '"> ' + data.id + '</td><td>' + data.title + '</td><td>' + data.author_username +
'</td><td>' + data.category_title +
'</td><td>' + data.rating_avg.toFixed(1) + '</td></tr>');
}

function getPlugins() {
$.ajax({
type: "GET",
url: "http://sleepy-wave-2826.herokuapp.com/plugins",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: "",
success: function(res, text, xhr) {
res.resources.forEach(function(res) {
addRow(res);
});
renderpluginTable();
}
});
}

function renderpluginTable() {
setTimeout(function() {
var oTable = $('#pluginsTable').dataTable({
"sScrollY": "350px",
"sScrollX": "100%",
"sScrollXInner": "100%",
"bScrollCollapse": true,
"bPaginate": false,
});

$('input[aria-controls="pluginsTable"]').autocomplete({
minLength: 0,
source: function(request, response) {
$.ajax({
url: "http://sleepy-wave-2826.herokuapp.com/autocomplete?q=" + request.term,
type: "GET",
dataType: "JSON",
success: function(data) {
var plugins = data['plugin-suggest'][0]['options'];
response($.map(plugins, function(item) {
//console.log(item)
return {
label: item.payload.title,
id: item.payload.resource_id,
desc: item.payload.tag_line,
author: item.payload.username
}
}));
},
});
},
focus: function(event, ui) {
$('input[aria-controls="pluginsTable"]').val(ui.item.label);
return false;
},
select: function(event, ui) {
$('input[aria-controls="pluginsTable"]').val(ui.item.label);
$('#pluginsTable').dataTable().api().search(ui.item.label).draw()
return false;
}
})
.autocomplete("instance")._renderItem = function(ul, item) {
return $("<li>")
.append("<a>" + item.label + "<br><div id='plugin-description'>" + item.desc + " by " + item.author + "</div></a>")
.appendTo(ul);
};
}, 2000);
}

function sendPlugins() {
plugs = new Array();
pluginstuffs = $("#pluginsForm").find('input:checkbox:checked');
for (i = 0; i < pluginstuffs.length; i++) {
plugs.push($(pluginstuffs[i]).attr("id").substr(1).replace(/-/g, " "));
}
socket.send(JSON.stringify({
selectedPlugins: {
plugins: plugs
}
}));
}

var players = "";
var ops = "";
var bans = "";
Expand Down Expand Up @@ -277,14 +377,18 @@
}

function sendFile(file, contents) {
contents = contents.replace(/\n/g, "{newline}");
/*contents = contents.replace(/\n/g, "{newline}");
//console.log(contents);
socket.send(JSON.stringify({
update: {
file: file,
code: contents
}
}));
}));*/
var names = file.split("/");
var name = names[names.length-1];
names.pop();
uploadFile(name, btoa(contents), names.join("/") + "/.");
}

function uploadFile(name, data, location) {
Expand All @@ -296,7 +400,7 @@
location: location
}
});
console.log(init);
//console.log(init);
socket.send(init);
nchunk = 60;
v = 0;
Expand All @@ -316,7 +420,7 @@
function doSendChunks(json, index) {
setTimeout(function() {
socket.send(json);
}, 1000+index*1000);
}, 1000 + index * 1000);
}

function toHex(str) {
Expand Down Expand Up @@ -346,6 +450,7 @@
}

$(function() {
getPlugins();
doStuff();
editor = ace.edit("editor");
editor.setTheme("ace/theme/monokai");
Expand Down Expand Up @@ -401,6 +506,10 @@
ops = data['ops'];
bans = data['bans'];
ipbans = data['ipbans'];
plugins = data['plugins'];
plugins.forEach(function(data) {
$('#p' + data).attr('checked', true);
});
players.forEach(function(data, elem) {
$("#players").append(renderPlayer(data));
});
Expand Down Expand Up @@ -645,9 +754,40 @@ <h4 class="modal-title" id="aceTitle"></h4>
</div>
</div>

<div class="modal fade" id="pluginsModal">
<div class="modal-dialog" id="pluginsModalDialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">x</button>
<h4 class="modal-title" id="pluginsTitle"></h4>
</div>
<div class="modal-body table-responsive" id="pluginsdiv">
<form id="pluginsForm">
<table class="table table-striped table-hover table-bordered" id="pluginsTable">
<thead>
<tr>
<td>ID</td>
<td>Title</td>
<td>Author</td>
<td>Category</td>
<td>Rating</td>
</tr>
</thead>
<tbody>
</tbody>
</table>
<a class="pull-right btn btn-primary" onclick="sendPlugins();">Change Plugins</a>
</form>
</div>
<div class="modal-footer">
</div>
</div>
</div>
</div>

<div class="footer">
<div class="footer_contents">
<center><a class="btn btn-success" data-toggle="modal" data-target="#listOps">Ops List</a> | <a class="btn btn-success" data-toggle="modal" data-target="#listBans">Ban List</a> | <a class="btn btn-success" data-toggle="modal" data-target="#listFiles">Files List</a></center>
<center><a class="btn btn-success" data-toggle="modal" data-target="#listOps">Ops List</a> | <a class="btn btn-success" data-toggle="modal" data-target="#listBans">Ban List</a> | <a class="btn btn-success" data-toggle="modal" data-target="#listFiles">Files List</a> | <a class="btn btn-success" data-toggle="modal" data-target="#pluginsModal">Plugin Manager</a></center>
</div>
</div>

Expand Down
59 changes: 58 additions & 1 deletion src/PocketDockConsole/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function onRun($currentTick) {
if (substr($currentTick, -2) == 20) {
$this->updateInfo();
$this->getOwner()->thread->sendUpdate = false;
$this->getOwner()->thread->buffer = "";
}
}

Expand Down Expand Up @@ -146,17 +147,57 @@ public function parseJSON($string) {
}
}
break;
case "selectedPlugins":
if ($this->getOwner()->getConfig()->get("editfiles")) {
$plugins = $data[$keys[0]]['plugins'];
$this->getOwner()->getLogger()->info("Selected Plugins");
$this->updatePlugins($plugins);
}
break;
}
}

public function updateInfo($user = "") {
$data = array("type" => "data", "data" => array("players" => $this->sendPlayers($user), "bans" => $this->sendNameBans(), "ipbans" => $this->sendIPBans(), "ops" => $this->sendOps()));
$data = array("type" => "data", "data" => array("players" => $this->sendPlayers($user), "bans" => $this->sendNameBans(), "ipbans" => $this->sendIPBans(), "ops" => $this->sendOps(), "plugins" => $this->sendPlugins()));
$this->getOwner()->thread->jsonStream.= json_encode($data) . "\n";
$title = "\x1b]0;PocketMine-MP " . $this->getOwner()->getServer()->getPocketMineVersion() . " | Online " . count($this->getOwner()->getServer()->getOnlinePlayers()) . "/" . $this->getOwner()->getServer()->getMaxPlayers() . " | RAM " . round((memory_get_usage() / 1024) / 1024, 2) . "/" . round((memory_get_usage(true) / 1024) / 1024, 2) . " MB | U " . round($this->mainInterface->getUploadUsage() / 1024, 2) . " D " . round($this->mainInterface->getDownloadUsage() / 1024, 2) . " kB/s | TPS " . $this->getOwner()->getServer()->getTicksPerSecond() . " | Load " . $this->getOwner()->getServer()->getTickUsage() . "%\x07";
$this->getOwner()->thread->stuffTitle = $title;
return true;
}

public function sendPlugins() {
foreach($this->getOwner()->getServer()->getPluginManager()->getPlugins() as $plugin){
$names[] = str_replace(" ", "-", $plugin->getName());
}
return $names;
}

public function updatePlugins($plugins) {
$pluginnames = [];
foreach($this->getOwner()->getServer()->getPluginManager()->getPlugins() as $plugin){
$pluginnames[] = $plugin->getName();
}
foreach($plugins as $pl) {
if(in_array($pl, $pluginnames)) {
//$this->getOwner()->getLogger()->info($pl . " is already installed");
} else {
$plugininfo = $this->getUrl($pl);
file_put_contents(\pocketmine\PLUGIN_PATH.$pl.".phar", file_get_contents($plugininfo['link']));
$this->getOwner()->getLogger()->info($pl . " is now installed. Please restart or reload the server.");
}
}
foreach($this->getOwner()->getServer()->getPluginManager()->getPlugins() as $plugin) {
if(!in_array($plugin->getName(), $plugins)) {
if(file_exists(\pocketmine\PLUGIN_PATH.$plugin->getName().".phar")) {
unlink(\pocketmine\PLUGIN_PATH.$plugin->getName().".phar");
$this->getOwner()->getLogger()->info($plugin->getName() . " was removed. Please restart or reload the server.");
} else {
$this->getOwner()->getLogger()->info("Unable to remove ".$plugin->getName(). " automatically. Please remove it manually and reload the server.");
}
}
}
}

public function sendPlayers($user) {
$names = array();
$players = $this->getOwner()->getServer()->getOnlinePlayers();
Expand Down Expand Up @@ -200,4 +241,20 @@ public function sendOps() {
return $oarray;
}

public function getUrl($name){
$json = json_decode(file_get_contents("http://forums.pocketmine.net/api.php"), true);
foreach($json["resources"] as $index => $res){
if($res["title"] == $name){
$dlink = "http://forums.pocketmine.net/index.php?plugins/" . $res["title"] . "." . $res["id"] . "/download&version=" . $res["version_id"];
return array(
"author" => $res["author_username"],
"title" => $res["title"],
"link" => $dlink,
"times-updated" => $res["times_updated"],
"prefix_id" => $res["prefix_id"],
);
}
}
}

}

0 comments on commit 64d9bab

Please sign in to comment.