Skip to content

Commit

Permalink
Blind commit of upcoming release
Browse files Browse the repository at this point in the history
  • Loading branch information
Dereku committed Jun 9, 2017
1 parent acfd976 commit cfadc6b
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 385 deletions.
16 changes: 7 additions & 9 deletions src/club/without/dereku/itemtooltips/ItemTooltips.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import java.util.Properties;
import java.util.logging.Level;
import java.util.stream.Collectors;
import org.bukkit.configuration.InvalidConfigurationException;
import org.apache.commons.io.FileUtils;
import org.bukkit.plugin.java.JavaPlugin;

/**
Expand All @@ -43,7 +43,6 @@ public class ItemTooltips extends JavaPlugin {

public final Properties keys = new Properties();
private String language;
private ResourceDownloader rd;
public List<String> worlds;

@Override
Expand All @@ -53,7 +52,7 @@ public void onEnable() {

if (!this.language.equals("en_US")) {
//TODO
this.downloadAndApplyLanguage("", this.language);
this.downloadAndApplyLanguage(this.language);
}

this.worlds = this.getConfig().getStringList("worlds");
Expand All @@ -80,14 +79,13 @@ public void onEnable() {
this.getLogger().info("Enabled.");
}

public void downloadAndApplyLanguage(String version, String lang) {
File file = new File(this.getDataFolder().toString() + File.separator + "lang" + File.separator + version, lang + ".lang");
public void downloadAndApplyLanguage(String lang) {
File file = FileUtils.getFile(this.getDataFolder().toString(), "lang", lang + ".lang");
if (!file.exists()) {
file.mkdir();
file.getParentFile().mkdirs();
try {
this.rd = new ResourceDownloader(this);
this.rd.downloadResource(version, lang, file);
} catch (IOException | InvalidConfigurationException | IllegalArgumentException ex) {
new ResourceDownloader(this).downloadResource(lang, file);
} catch (IOException | IllegalArgumentException ex) {
this.getLogger().log(Level.WARNING, "Failed to download " + file.getName(), ex);
this.getLogger().log(Level.WARNING, "Using en_US language.");
this.keys.clear();
Expand Down
12 changes: 3 additions & 9 deletions src/club/without/dereku/itemtooltips/Listeners.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,9 @@ public Listeners(ItemTooltips aThis) throws ClassNotFoundException, SecurityExce
this.plugin.getConfig().getString("format.withoutAmount", "%name%")
);

String pckg = Arrays.stream(Package.getPackages())
.filter(pk -> pk.getName().startsWith("net.minecraft.server"))
.map(pk -> pk.getName()).findFirst().orElse(null);

if (pckg == null) {
throw new RuntimeException("Failed to recognize nms version.");
}

String nmsVersion = pckg.substring(21);
String pckg = aThis.getServer().getClass().getPackage().getName();
String nmsVersion = pckg.substring(pckg.lastIndexOf('.') + 1);

this.nmsItemStack = ClassUtils.getClass("net.minecraft.server." + nmsVersion + ".ItemStack");
this.obcbCraftItemStack = ClassUtils.getClass("org.bukkit.craftbukkit." + nmsVersion + ".CraftItemStack");
this.asNMSCopy = this.obcbCraftItemStack.getMethod("asNMSCopy", ItemStack.class);
Expand Down
116 changes: 98 additions & 18 deletions src/club/without/dereku/itemtooltips/ResourceDownloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,57 +23,137 @@
*/
package club.without.dereku.itemtooltips;

import com.google.gson.Gson;
import com.google.gson.internal.LinkedTreeMap;
import com.google.gson.stream.JsonReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.logging.Level;
import org.apache.commons.io.FileUtils;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;

/**
*
* @author Dereku
*/
public class ResourceDownloader {

private final static String VERSIONS_LIST = "https://launchermeta.mojang.com/mc/game/version_manifest.json";
private final static String ASSETS_URL = "http://resources.download.minecraft.net/";
private final Gson gson = new Gson();
private final ItemTooltips plugin;
private final FileConfiguration configuration = new YamlConfiguration();

public ResourceDownloader(ItemTooltips plugin) throws IOException, InvalidConfigurationException {
public ResourceDownloader(ItemTooltips plugin) {
this.plugin = plugin;
try (InputStreamReader isr = new InputStreamReader(plugin.getResource("hashs.yml"))) {
this.configuration.load(isr);
}
}

/**
* Download locale file.
* @param version version of assets
* @param name Name of resource. Ex.: ru_RU, en_CA, etc.
*
* @param locale Name of resource. Ex.: ru_RU, en_CA, etc.
* @param destination Destination where to store file.
* @throws MalformedURLException
* @throws IOException
* @throws IOException
*/
public void downloadResource(String version, String name, File destination) throws MalformedURLException, IOException {
String hash;
if ((hash = this.configuration.getString(version + "." + name)) == null) {
throw new IllegalArgumentException("Resource with name \"" + name + "\" does not exists!");
public void downloadResource(String locale, File destination) throws MalformedURLException, IOException {
URL versionList = new URL(ResourceDownloader.VERSIONS_LIST);

VersionManifest vm;
try (
InputStream inputStream = versionList.openConnection().getInputStream();
InputStreamReader r = new InputStreamReader(inputStream);
JsonReader jr = new JsonReader(r)
) {
vm = this.gson.fromJson(jr, VersionManifest.class); //I hope this will works
}
RemoteClient latestRelease = vm.getLatestRelease();
URL assetsUrl = new URL(latestRelease.getUrl());

AssetIndex ai;
try (
InputStream inputStream = assetsUrl.openConnection().getInputStream();
InputStreamReader r = new InputStreamReader(inputStream);
JsonReader jr = new JsonReader(r)
) {
ai = this.gson.fromJson(jr, AssetIndex.class); //I hope this will works too
}
this.plugin.getLogger().log(Level.INFO, "Downloading {0}.lang (hash: {1})", new Object[]{name, hash});
String hash = ai.getLocaleHash(locale);
this.plugin.getLogger().log(Level.INFO, "Downloading {0}.lang (hash: {1})", new Object[]{locale, hash});
FileUtils.copyURLToFile(new URL(ResourceDownloader.ASSETS_URL + this.createPathFromHash(hash)), destination);
}

/**
* From Mojang, with love.
* From Mojang, with love.
*
* @param hash
* @return
* @return
*/
private String createPathFromHash(String hash) {
return hash.substring(0, 2) + "/" + hash;
}

/*
Gson serialization
*/
class VersionManifest {

LinkedTreeMap<String, String> latest;
ArrayList<RemoteClient> versions;

public RemoteClient getLatestRelease() {
String release = this.latest.get("release");
for (RemoteClient c : this.versions) {
if (c.getId().equals(release)) {
return c;
}
}

throw new IllegalArgumentException(release + " does not exists. There something is definitely wrong.");
}
}

class RemoteClient {

String id, url;
Object type, time, releaseTime;

public String getId() {
return id;
}

public String getUrl() {
return url;
}
}

class ClientVersion {

LinkedTreeMap<String, String> assetIndex;
Object assets, downloads, id, libraries, logging, mainClass;
Object minecraftArguments, minimumLauncherVersion, releaseTime;
Object time, type;

public String getAssetUrl() {
return this.assetIndex.get("url");
}
}

class AssetIndex {

private final static String PATH = "minecraft/lang/%s.lang";
LinkedTreeMap<String, LinkedTreeMap<String, String>> objects;

public String getLocaleHash(String locale) {
LinkedTreeMap<String, String> asset
= this.objects.get(String.format(PATH, locale.toLowerCase()));
if (asset == null) {
throw new IllegalArgumentException("Locale " + locale + " does not exists!");
}
return asset.get("hash");
}
}
}

0 comments on commit cfadc6b

Please sign in to comment.