diff --git a/package.json b/package.json index bbb836ea5..22e599aee 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ "lodash": "^4.17.11", "lodash-webpack-plugin": "^0.11.0", "ls-cache": "^0.2.3", + "lscache": "^1.3.0", "minimatch": "3.0.4", "mkdirp": "^0.5.1", "moment": "^2.22.2", diff --git a/src/js/ui/ServerInfo.jsx b/src/js/ui/ServerInfo.jsx index d56207d91..60cc8ab9f 100644 --- a/src/js/ui/ServerInfo.jsx +++ b/src/js/ui/ServerInfo.jsx @@ -16,6 +16,7 @@ import {round} from "lodash/math"; import data from "../data"; import _ from "lodash"; import replaceColorCodes from "../mccolors"; +import lscache from "lscache"; function gcSummary() { const system = data.timingsMaster.system; @@ -115,11 +116,19 @@ export default class ServerInfo extends React.PureComponent { constructor(props, ctx) { super(props, ctx); this.state = { - dataReady: false + dataReady: false, + latest: null, + label: '', }; data.onReady(() => this.setState({dataReady: true})); } + componentDidUpdate() { + if (this.state.dataReady) { + this.checkIfOutdated(); + } + } + render() { if (!this.state.dataReady) { return
; @@ -157,7 +166,7 @@ export default class ServerInfo extends React.PureComponent { : null} Version - {info.version} + {info.version}
{this.state.label} GC @@ -173,6 +182,57 @@ export default class ServerInfo extends React.PureComponent { ) } + checkIfOutdated() { + const info = data.timingsMaster; + const version = info.version; + // Make sure it's a good copy of paper + if (!version.match(/git-Paper(\d+)/)) { + return; + } + // Check if it's in cache or not + if (lscache.get('latest_build') !== null) { + // Check directly with cache + this.checkIfOnLatest(version, lscache.get('latest_build')); + } else { + // Get the latest from paper api + this.getLatestBuild(version); + } + // Set the label + if (this.state.latest) { + this.setState({ + label: ✓Latest Build + }); + } else { + this.setState({ + label: ✗Outdated Build UPDATE NOW + }); + } + } + + // Get the latest build from paper + getLatestBuild(version) { + fetch('https://papermc.io/api/v1/paper/1.15.2').then((response) => response.json()).then((responseJson) => { + const latest = responseJson.builds.latest; + lscache.set('latest_build', latest, 60); + this.checkIfOnLatest(version, latest); + }).catch((error) => { + console.error(error); + }); + } + + // Check if they are on the latest build + checkIfOnLatest(version, latest) { + if (version.indexOf(latest) !== -1) { + this.setState({ + latest: true + }); + } else { + this.setState({ + latest: false + }); + } + } + showOnlineMode() { const info = data.timingsMaster; const config = info.config; diff --git a/yarn.lock b/yarn.lock index 6e6f036f4..85a413fe4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3681,6 +3681,11 @@ ls-cache@^0.2.3: version "0.2.3" resolved "https://registry.yarnpkg.com/ls-cache/-/ls-cache-0.2.3.tgz#0cdaf7338d395768cdac0af611e5a8ef2bcaaf91" +lscache@^1.3.0: + version "1.3.0" + resolved "https://registry.yarnpkg.com/lscache/-/lscache-1.3.0.tgz#2221fd39f3393d2dfbc6ed2231beb52e11e43396" + integrity sha512-0JwzMSSu3fd3m8QQVbqIxzXywkNLQvgdNehuEtZ66v7f89ybpkZX+WN45SkvChP4AqUPSpDPJKHsAqStOhHgUA== + make-iterator@^1.0.0: version "1.0.1" resolved "https://registry.yarnpkg.com/make-iterator/-/make-iterator-1.0.1.tgz#29b33f312aa8f547c4a5e490f56afcec99133ad6"