Add api to get MCVersion and Improve MojangAPI

Former-commit-id: fba7ebb23363f75b7445b13773a25d57e1e919f4
This commit is contained in:
Ghost_chu committed 2020-01-10 17:11:16 +08:00
1 parent a555974053
commit 8fda44b091
5 files changed
+205 -25

No files matched your search

@@ -580,6 +580,7 @@ public class QuickShop extends JavaPlugin {
this.displayAutoDespawnWatcher.runTaskTimerAsynchronously(this, 0, getConfig().getInt("shop.display-check-time"));
}
}
getLogger().severe("MCVER:"+ReflectFactory.getServerVersion());
}
/**
@@ -599,7 +600,7 @@ public class QuickShop extends JavaPlugin {
throw new RuntimeException("Server must be Spigot based, Don't use CraftBukkit!");
}
if (getServer().getName().toLowerCase().contains("catserver")) {
if (getServer().getName().toLowerCase().contains("catserver") || Util.isClassAvailable("moe.luohuayu.CatServer") ||Util.isClassAvailable("catserver.server.CatServer")) {
// Send FATAL ERROR TO CatServer's users.
getLogger().severe("FATAL: QSRR can't run on CatServer Community/Personal/Pro/Async");
throw new RuntimeException("QuickShop doen't support CatServer");
@@ -620,9 +621,9 @@ public class QuickShop extends JavaPlugin {
}
String nmsVersion = Util.getNMSVersion();
IncompatibleChecker incompatibleChecker = new IncompatibleChecker();
getLogger().info("Running QuickShop-Reremake on NMS version " + nmsVersion + " For Minecraft version " + Util.getMinecraftVersionFromNMSVersion(Util.getNMSVersion()));
getLogger().info("Running QuickShop-Reremake on NMS version " + nmsVersion + " For Minecraft version " + ReflectFactory.getServerVersion());
if (incompatibleChecker.isIncompatible(nmsVersion)) {
throw new RuntimeException("Your Minecraft version is nolonger supported: " + nmsVersion);
throw new RuntimeException("Your Minecraft version is nolonger supported: " + ReflectFactory.getServerVersion()+" ("+nmsVersion+")");
}
}
@@ -19,6 +19,7 @@
package org.maxgamer.quickshop.Util.MojangAPI;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
@@ -28,36 +29,61 @@ import org.maxgamer.quickshop.NonQuickShopStuffs.com.sk89q.worldedit.util.net.Ht
import org.maxgamer.quickshop.Util.MsgUtil;
import org.maxgamer.quickshop.Util.Util;
import java.io.IOException;
import java.net.URL;
public class MojangAPI {
final String versionManifestUrl = "https://launchermeta.mojang.com/mc/game/version_manifest.json";
final String assetsUrl = "https://resources.download.minecraft.net/";
final String pathTemplate = "minecraft/lang/{0}.json";
final Gson gson = new Gson();
/**
* Return Minecraft version manifest.
*
* @return Version Manifest, may be null when failed to get.
*/
@Nullable
public String getVersionManifest(){
try{
public String getVersionManifest() {
try {
return HttpRequest.get(new URL(versionManifestUrl))
.expectResponseCode(200)
.execute()
.returnContent()
.asString("UTF-8").trim();
}catch (Exception e){
} catch (Exception e) {
Util.debugLog(e.getMessage());
return null;
}
}
@Nullable
public String getLanguageFileFromAsset(@NotNull String versionManifestJson,@NotNull String languageCode){
public String getVersionJson(@NotNull String versionManifest, @NotNull String mcVer) {
VersionList list = gson.fromJson(versionManifest, VersionList.class);
for (VersionList.VersionsBean mcv :
list.getVersions()) {
if (mcv.getId().equals(mcVer)) {
try {
return HttpRequest.get(new URL(mcv.getUrl()))
.expectResponseCode(200)
.execute()
.returnContent()
.asString("UTF-8").trim();
} catch (IOException e) {
Util.debugLog(e.getMessage());
return null;
}
}
}
return null;
}
@Nullable
public String getLanguageFileFromAsset(@NotNull String versionJson, @NotNull String languageCode) {
try {
languageCode = languageCode.toLowerCase().trim();
JsonObject object = new JsonParser().parse(versionManifestJson).getAsJsonObject();
JsonObject object = new JsonParser().parse(versionJson).getAsJsonObject();
JsonElement element = object.get(MsgUtil.fillArgs(pathTemplate, languageCode));
String hash = element.getAsString();
@@ -66,7 +92,7 @@ public class MojangAPI {
.expectResponseCode(200)
.returnContent()
.asString("UTF-8").trim();
}catch (Exception e){
} catch (Exception e) {
Util.debugLog(e.getMessage());
return null;
}
@@ -0,0 +1,131 @@
/*
* This file is a part of project QuickShop, the name is VersionList.java
* Copyright (C) Ghost_chu <https://github.com/Ghost-chu>
* Copyright (C) Bukkit Commons Studio and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.maxgamer.quickshop.Util.MojangAPI;
import java.util.List;
public class VersionList {
/**
* latest : {"release":"1.15.1","snapshot":"1.15.1"}
* versions : [{"id":"1.15.1","type":"release","url":"https://launchermeta.mojang.com/v1/packages/57c1b5f7376febf269f7e555f96def8f1a43bb76/1.15.1.json","time":"2019-12-16T10:33:36+00:00","releaseTime":"2019-12-16T10:29:47+00:00"},{"id":"1.15.1-pre1","type":"snapshot","url":"https://launchermeta.mojang.com/v1/packages/d239ea078ac8b83a3c8d58fa4350aa11ce846884/1.15.1-pre1.json","time":"2019-12-16T09:54:41+00:00","releaseTime":"2019-12-12T14:02:30+00:00"},{"id":"1.15","type":"release","url":"https://launchermeta.mojang.com/v1/packages/443d30b7da718d9aa87d3f3c0c899d396658f709/1.15.json","time":"2019-12-16T09:54:41+00:00","releaseTime":"2019-12-09T13:13:38+00:00"},{"id":"1.15-pre7","type":"snapshot","url":"https://launchermeta.mojang.com/v1/packages/58607dd6ec06293aaa114637252bdacabaeabdeb/1.15-pre7.json","time":"2019-12-16T09:54:41+00:00","releaseTime":"2019-12-09T12:14:11+00:00"},{"id":"1.15-pre6","type":"snapshot","url":"https://launchermeta.mojang.com/v1/packages/5321d1761f24669cdbf3b150935891c5959df1d0/1.15-pre6.json","time":"2019-12-16T09:54:41+00:00","releaseTime":"2019-12-06T12:04:30+00:00"},{"id":"1.15-pre5","type":"snapshot","url":"https://launchermeta.mojang.com/v1/packages/333c5fabcd618c8ac91be79d694f6907450cbf78/1.15-pre5.json","time":"2019-12-16T09:54:41+00:00","releaseTime":"2019-12-05T13:20:00+00:00"},{"id":"1.15-pre4","type":"snapshot","url":"https://launchermeta.mojang.com/v1/packages/ddc2328ebbca2cecae0466f4ea25599b93efbb21/1.15-pre4.json","time":"2019-12-16T09:54:41+00:00","releaseTime":"2019-12-03T12:24:24+00:00"},{"id":"1.15-pre3","type":"snapshot","url":"https://launchermeta.mojang.com/v1/packages/8a74cef7b525cc02c4c44dd5d56d3cac1ea67413/1.15-pre3.json","time":"2019-12-16T09:54:41+00:00","releaseTime":"2019-11-28T17:17:50+00:00"},{"id":"1.15-pre2","type":"snapshot","url":"https://launchermeta.mojang.com/v1/packages/4c41453e6c92ca22007e16a167010c2f9b692bc1/1.15-pre2.json","time":"2019-12-16T09:54:41+00:00","releaseTime":"2019-11-25T18:09:38+00:00"},{"id":"1.15-pre1","type":"snapshot","url":"https://launchermeta.mojang.com/v1/packages/d0875206ed354177e6a33b0d8cb819d5fa995fbe/1.15-pre1.json","time":"2019-12-16T09:54:41+00:00","releaseTime":"2019-11-21T17:01:17+00:00"},{"id":"19w46b","type":"snapshot","url":"https://launchermeta.mojang.com/v1/packages/115d0942f228e3cb4e6552dfcf17ea97ab1de2cb/19w46b.json","time":"2019-12-16T09:54:41+00:00","releaseTime":"2019-11-14T13:29:24+00:00"},{"id":"19w46a","type":"snapshot","url":"https://launchermeta.mojang.com/v1/packages/8885f644826cb7efb2bc5fde58bf7eb5ca7a7200/19w46a.json","time":"2019-12-16T09:54:41+00:00","releaseTime":"2019-11-13T16:37:46+00:00"},{"id":"19w45b","type":"snapshot","url":"https://launchermeta.mojang.com/v1/packages/51a441c591c863e6de37ef4a176a02803ed0bc67/19w45b.json","time":"2019-12-16T09:54:41+00:00","releaseTime":"2019-11-08T12:42:44+00:00"},{"id":"19w45a","type":"snapshot","url":"https://launchermeta.mojang.com/v1/packages/206af9f137dd4f7cc29250375985e8702a7de227/19w45a.json","time":"2019-12-16T09:54:41+00:00","releaseTime":"2019-11-07T16:19:20+00:00"},{"id":"19w44a","type":"snapshot","url":"https://launchermeta.mojang.com/v1/packages/d834604fac8cd74dcf40a4445571b538500a2db6/19w44a.json","time":"2019-12-16T09:54:41+00:00","releaseTime":"2019-10-30T15:31:44+00:00"},{"id":"19w42a","type":"snapshot","url":"https://launchermeta.mojang.com/v1/packages/c4e966161b97320d728f1789d55e0fd18214e29e/19w42a.json","time":"2019-12-16T09:54:41+00:00","releaseTime":"2019-10-16T15:30:39+00:00"},{"id":"19w41a","type":"snapshot","url":"https://launchermeta.mojang.com/v1/packages/7187270992ac12c58e284abc520939e36217f69d/19w41a.json","time":"2019-12-16T09:54:41+00:00","releaseTime":"2019-10-09T15:21:35+00:00"},{"id":"19w40a","type":"snapshot","url":"https://launchermeta.mojang.com/v1/packages/f4eaa67b689254368bb98430efd7f58ff48cc919/19w40a.json","time":"2019-12-16T09:54:41+00:00","releaseTime":"2019-10-02T13:40:26+00:00"},{"id":"19w39a","type":"snapshot","url":"https://launchermeta.mojang.com/v1/packages/380fc859577e4e2de5265fa757e722c706343557/19w39a.json","time":"2019-12-16T09:54:41+00:00","releaseTime":"2019-09-27T10:13:33+00:00"},{"id":"19w38b","type":"snapshot","url":"https://launchermeta.mojang.com/v1/packages/68527e31bcaa07afb63518cec727ffc28a0d2eac/19w38b.json","time":"2019-12-16T09:54:41+00:00","releaseTime":"2019-09-18T14:59:13+00:00"},{"id":"19w38a","type":"snapshot","url":"https://launchermeta.mojang.com/v1/packages/9d7951d20d310617748c1419c9bd8018e1248dd6/19w38a.json","time":"2019-12-16T09:54:41+00:00","releaseTime":"2019-09-18T10:03:22+00:00"},{"id":"19w37a","type":"snapshot","url":"https://launchermeta.mojang.com/v1/packages/39e4f1ed9ac6e84b8e1f357f28eb3d44d346217e/19w37a.json","time":"2019-12-16T09:54:41+00:00","releaseTime":"2019-09-11T11:46:44+00:00"},{"id":"19w36a","type":"snapshot","url":"https://launchermeta.mojang.com/v1/packages/0c3334342c489e505e80a419d2f1e3c9b1e3b605/19w36a.json","time":"2019-12-16T09:54:41+00:00","releaseTLine truncated
*/
private LatestBean latest;
private List<VersionsBean> versions;
public LatestBean getLatest() {
return latest;
}
public void setLatest(LatestBean latest) {
this.latest = latest;
}
public List<VersionsBean> getVersions() {
return versions;
}
public void setVersions(List<VersionsBean> versions) {
this.versions = versions;
}
public static class LatestBean {
/**
* release : 1.15.1
* snapshot : 1.15.1
*/
private String release;
private String snapshot;
public String getRelease() {
return release;
}
public void setRelease(String release) {
this.release = release;
}
public String getSnapshot() {
return snapshot;
}
public void setSnapshot(String snapshot) {
this.snapshot = snapshot;
}
}
public static class VersionsBean {
/**
* id : 1.15.1
* type : release
* url : https://launchermeta.mojang.com/v1/packages/57c1b5f7376febf269f7e555f96def8f1a43bb76/1.15.1.json
* time : 2019-12-16T10:33:36+00:00
* releaseTime : 2019-12-16T10:29:47+00:00
*/
private String id;
private String type;
private String url;
private String time;
private String releaseTime;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getReleaseTime() {
return releaseTime;
}
public void setReleaseTime(String releaseTime) {
this.releaseTime = releaseTime;
}
}
}
@@ -0,0 +1,38 @@
/*
* This file is a part of project QuickShop, the name is ReflectFactory.java
* Copyright (C) Ghost_chu <https://github.com/Ghost-chu>
* Copyright (C) Bukkit Commons Studio and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.maxgamer.quickshop.Util;
import org.bukkit.Bukkit;
import java.lang.reflect.Field;
public class ReflectFactory {
public static String getServerVersion(){
try {
Field consoleField = Bukkit.getServer().getClass().getDeclaredField("console");
consoleField.setAccessible(true); // protected
Object console = consoleField.get(Bukkit.getServer()); // dedicated server
return String.valueOf(console.getClass().getSuperclass().getMethod("getVersion").invoke(console));
}catch (Exception e){
e.printStackTrace();
return "Unknown";
}
}
}
@@ -1282,22 +1282,6 @@ public class Util {
}
}
/**
* Get Minecraft version without patch number.
* E.g 1.12,1.13,1.14,1.15
* @param NMSVersion Util.getNMSVersion()
* @return The Minecraft Version
*/
@Nullable
public static String getMinecraftVersionFromNMSVersion(@NotNull String NMSVersion){
//v1_11_R1
String minecrftVersion = MsgUtil.getSubString(NMSVersion,"v","_R");
if(minecrftVersion.isEmpty()){
return null;
}
return minecrftVersion.replace("_",".");
}
/**
* Call a event and check it is cancelled.
* @param event The event implement the Cancellable interface.