diff --git a/src/club/without/dereku/itemtooltips/ItemTooltips.java b/src/club/without/dereku/itemtooltips/ItemTooltips.java index fdf8bcd..fe1826e 100644 --- a/src/club/without/dereku/itemtooltips/ItemTooltips.java +++ b/src/club/without/dereku/itemtooltips/ItemTooltips.java @@ -48,10 +48,9 @@ public class ItemTooltips extends JavaPlugin { @Override public void onEnable() { this.saveDefaultConfig(); - this.language = this.getConfig().getString("lang", "en_US"); - - if (!this.language.equals("en_US")) { - //TODO + this.language = this.getConfig().getString("lang", "en_us").toLowerCase(); + + if (!this.language.equals("en_us")) { this.downloadAndApplyLanguage(this.language); } @@ -59,13 +58,13 @@ public void onEnable() { if (this.worlds.isEmpty()) { this.worlds.addAll( this.getServer().getWorlds().stream() - .map(w -> w.getName()) - .collect(Collectors.toList()) + .map(w -> w.getName()) + .collect(Collectors.toList()) ); this.getConfig().set("worlds", this.worlds); this.saveConfig(); } - + Listeners listeners; try { listeners = new Listeners(this); @@ -74,17 +73,18 @@ public void onEnable() { this.getPluginLoader().disablePlugin(this); return; } - + this.getServer().getPluginManager().registerEvents(listeners, this); this.getLogger().info("Enabled."); } - public void downloadAndApplyLanguage(String lang) { + private void downloadAndApplyLanguage(String lang) { File file = FileUtils.getFile(this.getDataFolder().toString(), "lang", lang + ".lang"); if (!file.exists()) { file.getParentFile().mkdirs(); try { new ResourceDownloader(this).downloadResource(lang, file); + this.loadLanguage(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."); @@ -95,9 +95,10 @@ public void downloadAndApplyLanguage(String lang) { this.loadLanguage(file); } - public void loadLanguage(File file) { + private void loadLanguage(File file) { Charset charset = Charset.forName("UTF-8"); - try (InputStreamReader is = new InputStreamReader(new FileInputStream(file), charset)) { + try (FileInputStream fis = new FileInputStream(file); + InputStreamReader is = new InputStreamReader(fis, charset)) { this.keys.load(is); } catch (IOException ex) { this.getLogger().log(Level.WARNING, "Failed to load " + file.getName(), ex); diff --git a/src/club/without/dereku/itemtooltips/Listeners.java b/src/club/without/dereku/itemtooltips/Listeners.java index d735e58..0c88660 100644 --- a/src/club/without/dereku/itemtooltips/Listeners.java +++ b/src/club/without/dereku/itemtooltips/Listeners.java @@ -25,7 +25,6 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import java.util.Arrays; import java.util.logging.Level; import org.apache.commons.lang3.ClassUtils; import org.bukkit.ChatColor; @@ -58,7 +57,7 @@ public class Listeners implements Listener { public Listeners(ItemTooltips aThis) throws ClassNotFoundException, SecurityException, NoSuchMethodException { this.plugin = aThis; this.withAmount = ChatColor.translateAlternateColorCodes('&', - this.plugin.getConfig().getString("format.withAmount", "%name%") + this.plugin.getConfig().getString("format.withAmount", "%name% x%amount%") ); this.withoutAmount = ChatColor.translateAlternateColorCodes('&', this.plugin.getConfig().getString("format.withoutAmount", "%name%") @@ -68,7 +67,7 @@ public Listeners(ItemTooltips aThis) throws ClassNotFoundException, SecurityExce 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.obcbCraftItemStack = ClassUtils.getClass("org.bukkit.craftbukkit." + nmsVersion + ".inventory.CraftItemStack"); this.asNMSCopy = this.obcbCraftItemStack.getMethod("asNMSCopy", ItemStack.class); this.itemStack_getName = this.nmsItemStack.getDeclaredMethod("getName", (Class[]) null); this.itemStack_getI18n = this.nmsItemStack.getDeclaredMethod("a", (Class[]) null); @@ -105,10 +104,10 @@ private String getName(Item item) { try { Object nmsis = this.asNMSCopy.invoke(null, item.getItemStack()); if (this.plugin.keys.isEmpty()) { - return (String) this.itemStack_getName.invoke(nmsis, (Object) null); + return (String) this.itemStack_getName.invoke(nmsis, new Object[0]); } - i18n = (String) this.itemStack_getI18n.invoke(nmsis, (Object) null); - } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { + i18n = (String) this.itemStack_getI18n.invoke(nmsis, new Object[0]); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { this.plugin.getLogger().log(Level.WARNING, "Failed to get name", ex); return null; } @@ -121,14 +120,13 @@ private String getName(Item item) { return this.plugin.keys.getProperty(out, out); } - public String getBannerKey(Item item) { + //TODO: Remove this shit + private String getBannerKey(Item item) { if (!item.getItemStack().getType().equals(Material.BANNER)) { return null; } StringBuilder out = new StringBuilder(); BannerMeta bm = (BannerMeta) item.getItemStack().getItemMeta(); - System.out.println(item + ": " + item.getName() + ", " + bm); - System.out.println(bm.getBaseColor()); try { out.append(item.getName().replace("tile.", "")) .append(".") diff --git a/src/club/without/dereku/itemtooltips/ResourceDownloader.java b/src/club/without/dereku/itemtooltips/ResourceDownloader.java index ce31ace..2182f12 100644 --- a/src/club/without/dereku/itemtooltips/ResourceDownloader.java +++ b/src/club/without/dereku/itemtooltips/ResourceDownloader.java @@ -34,6 +34,8 @@ import java.net.URL; import java.util.ArrayList; import java.util.logging.Level; +import java.util.logging.Logger; +import java.lang.reflect.Type; import org.apache.commons.io.FileUtils; /** @@ -52,7 +54,6 @@ public ResourceDownloader(ItemTooltips plugin) { } /** - * Download locale file. * * @param locale Name of resource. Ex.: ru_RU, en_CA, etc. * @param destination Destination where to store file. @@ -60,32 +61,22 @@ public ResourceDownloader(ItemTooltips plugin) { * @throws IOException */ 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 - } + VersionManifest vm = this.downloadObject(new URL(ResourceDownloader.VERSIONS_LIST), VersionManifest.class); + ClientVersion client = this.downloadObject(new URL(vm.getLatestRelease().getUrl()), ClientVersion.class); + AssetIndex ai = this.downloadObject(new URL(client.getAssetUrl()), AssetIndex.class); 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); } + private T downloadObject(URL url, Class object) throws IOException { + try (InputStream inputStream = url.openConnection().getInputStream(); + InputStreamReader r = new InputStreamReader(inputStream); + JsonReader jr = new JsonReader(r)) { + return this.gson.fromJson(jr, object); + } + } + /** * From Mojang, with love. * diff --git a/src/config.yml b/src/config.yml index 98fdb27..b565c5e 100644 --- a/src/config.yml +++ b/src/config.yml @@ -1,4 +1,4 @@ -lang: en_US +lang: en_us format: withAmount: "%name% &7[x%amount%]" withoutAmount: "%name%" diff --git a/src/plugin.yml b/src/plugin.yml index 464a37f..6fe85ab 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -1,5 +1,5 @@ name: ItemTooltips -version: 2.0.0-dev +version: 2.0.0 author: Dereku email: [dereku@default.im, dereku@without.club] site: https://github.com/Dereku/ItemTooltips