diff --git a/CraftyController/CraftyController.php b/CraftyController/CraftyController.php new file mode 100644 index 0000000000..b44a6fd0fd --- /dev/null +++ b/CraftyController/CraftyController.php @@ -0,0 +1,135 @@ +jar = new \GuzzleHttp\Cookie\CookieJar; // Uncomment if cookies need to be set + } + + private function getToken() + { + $res = parent::execute($this->url('api/v2/auth/login'), $this->authAttrs()); + $data = json_decode($res->getBody()); + if ($data->status == 'ok') { + $this->token = $data->data->token; + } + } + + public function test() + { + try { + $this->getToken(); + echo "Successfully communicated with the API"; + } catch (Exception $err) { + echo $err->getMessage(); + } + } + + public function livestats() + { + $status = "inactive"; + $this->getToken(); + + $res = parent::execute( + $this->url('api/v2/servers'), + attrs: $this->attrs(), + overridemethod: 'GET' + ); + $details = json_decode($res->getBody()); + + $vars['servers_total'] = count($details->data); + + $servers_online = 0; + $players_online = 0; + $mem = 0; + + foreach ($details->data as $server) { + $server_res = parent::execute( + $this->url('api/v2/servers/' . $server->server_id . '/stats'), + attrs: $this->attrs(), + overridemethod: 'GET' + ); + $server_details = json_decode($server_res->getBody()); + $players_online += $server_details->data->online; + $mem += $this->decodeSizeToGB($server_details->data->mem); + if ($server_details->data->running == true) { + $servers_online++; + } + } + + $vars['servers_online'] = $servers_online; + $vars['players_online'] = $players_online; + $vars['mem'] = $mem; + + return parent::getLiveStats($status, $vars); + } + + private function authAttrs() + { + $ignoreTls = $this->getConfigValue("ignore_tls", false); + return [ + "body" => json_encode([ + "username" => $this->getConfigValue("username", null), + "password" => $this->getConfigValue("password", null) + ]), + "verify" => ($ignoreTls ? false : true) + ]; + } + + + public function attrs() + { + $ignoreTls = $this->getConfigValue("ignore_tls", false); + return [ + "headers" => [ + "content-type" => "application/json", + "Authorization" => "Bearer " . $this->token, + ], + "verify" => ($ignoreTls ? false : true) + ]; + } + + public function url($endpoint) + { + $api_url = parent::normaliseurl($this->config->url) . $endpoint; + return $api_url; + } + + private function getConfigValue($key, $default = null) + { + return isset($this->config) && isset($this->config->$key) + ? $this->config->$key + : $default; + } + + private function decodeSizeToGB($size) + { + $units = [ + 'B' => 1 / (1024 * 1024 * 1024), // Convert bytes to GB + 'KB' => 1 / (1024 * 1024), // Convert KB to GB + 'MB' => 1 / 1024, // Convert MB to GB + 'GB' => 1, // GB is the base unit + 'TB' => 1024, // Convert TB to GB + 'PB' => 1024 * 1024, // Convert PB to GB + ]; + + if (preg_match('/^([\d\.]+)\s*([KMGTP]?B)$/i', strtoupper($size), $matches)) { + $value = (float)$matches[1]; + $unit = $matches[2]; + + return round($value * ($units[$unit] ?? 1), 1); + } + + return false; // Invalid format + } +} diff --git a/CraftyController/app.json b/CraftyController/app.json new file mode 100644 index 0000000000..edd052843e --- /dev/null +++ b/CraftyController/app.json @@ -0,0 +1,10 @@ +{ + "appid": "200fe80f6b54e75f36a0ae671663f47b417de384", + "name": "Crafty Controller", + "website": "https://craftycontrol.com", + "license": "GNU General Public License v3.0 or later", + "description": "Crafty Controller is a free and open-source Minecraft launcher and manager that allows users to start and administer Minecraft servers from a user-friendly interface.", + "enhanced": true, + "tile_background": "dark", + "icon": "craftycontroller.png" +} diff --git a/CraftyController/config.blade.php b/CraftyController/config.blade.php new file mode 100644 index 0000000000..93635e82e0 --- /dev/null +++ b/CraftyController/config.blade.php @@ -0,0 +1,36 @@ +

{{ __('app.apps.config') }} ({{ __('app.optional') }}) @include('items.enable')

+
+
+ + {!! Form::text('config[override_url]', isset($item) ? $item->getconfig()->override_url : null, array('placeholder' => __('app.apps.override'), 'id' => 'override_url', 'class' => 'form-control')) !!} +
+
+ + {!! Form::text('config[username]', isset($item) ? $item->getconfig()->username : null, array('placeholder' => __('app.apps.username'), 'data-config' => 'username', 'class' => 'form-control config-item')) !!} +
+
+ + {!! Form::input('password', 'config[password]', '', ['placeholder' => __('app.apps.password'), 'data-config' => 'password', 'class' => 'form-control config-item']) !!} +
+
+ +
+ {!! Form::hidden('config[ignore_tls]', 0, ['class' => 'config-item', 'data-config' => 'ignore_tls']) !!} + +
+
+
+ +
+
+ diff --git a/CraftyController/craftycontroller.png b/CraftyController/craftycontroller.png new file mode 100644 index 0000000000..ec39e3fd29 Binary files /dev/null and b/CraftyController/craftycontroller.png differ diff --git a/CraftyController/livestats.blade.php b/CraftyController/livestats.blade.php new file mode 100644 index 0000000000..a369ff6643 --- /dev/null +++ b/CraftyController/livestats.blade.php @@ -0,0 +1,10 @@ + \ No newline at end of file