Skip to content

Commit

Permalink
2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
BuildTools committed Jun 9, 2017
1 parent cfadc6b commit ba5d962
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 44 deletions.
23 changes: 12 additions & 11 deletions src/club/without/dereku/itemtooltips/ItemTooltips.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,23 @@ 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);
}

this.worlds = this.getConfig().getStringList("worlds");
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);
Expand All @@ -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.");
Expand All @@ -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);
Expand Down
16 changes: 7 additions & 9 deletions src/club/without/dereku/itemtooltips/Listeners.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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%")
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
Expand All @@ -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(".")
Expand Down
35 changes: 13 additions & 22 deletions src/club/without/dereku/itemtooltips/ResourceDownloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -52,40 +54,29 @@ 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.
* @throws MalformedURLException
* @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 extends Object> T downloadObject(URL url, Class<T> 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.
*
Expand Down
2 changes: 1 addition & 1 deletion src/config.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
lang: en_US
lang: en_us
format:
withAmount: "%name% &7[x%amount%]"
withoutAmount: "%name%"
Expand Down
2 changes: 1 addition & 1 deletion src/plugin.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit ba5d962

Please sign in to comment.