Skip to content

Commit

Permalink
Woohoo
Browse files Browse the repository at this point in the history
  • Loading branch information
Dereku authored and BuildTools committed Jun 6, 2017
1 parent 5d084a1 commit acfd976
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 449 deletions.
49 changes: 25 additions & 24 deletions src/club/without/dereku/itemtooltips/ItemTooltips.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
*/
package club.without.dereku.itemtooltips;

import club.without.dereku.itemtooltips.implementations.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import org.bukkit.World;
import java.util.stream.Collectors;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.plugin.java.JavaPlugin;

Expand All @@ -41,44 +41,45 @@
*/
public class ItemTooltips extends JavaPlugin {

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

@Override
public void onEnable() {
this.saveDefaultConfig();
this.language = this.getConfig().getString("lang", "en_US");
this.impl = Implementation.getImpl(this.getServer().getBukkitVersion());
if (this.impl == null) {
this.getLogger().info("Not implemented yet.");
this.getPluginLoader().disablePlugin(this);
return;
}

this.getLogger().log(Level.INFO, "Using implementation with version {0}", this.impl.getVersion());

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

this.worlds = this.getConfig().getStringList("worlds");
if (worlds.isEmpty()) {
for (World world : this.getServer().getWorlds()) {
worlds.add(world.getName());
}
if (this.worlds.isEmpty()) {
this.worlds.addAll(
this.getServer().getWorlds().stream()
.map(w -> w.getName())
.collect(Collectors.toList())
);
this.getConfig().set("worlds", this.worlds);
this.saveConfig();
}
this.getServer().getPluginManager().registerEvents(new Listeners(this), this);

Listeners listeners;
try {
listeners = new Listeners(this);
} catch (ClassNotFoundException | SecurityException | NoSuchMethodException ex) {
this.getLogger().log(Level.SEVERE, "Failed to init listeners", ex);
this.getPluginLoader().disablePlugin(this);
return;
}

this.getServer().getPluginManager().registerEvents(listeners, this);
this.getLogger().info("Enabled.");
}

public Implementation getImpl() {
return this.impl;
}

public void downloadAndApplyLanguage(String version, String lang) {
File file = new File(this.getDataFolder().toString() + File.separator + "lang" + File.separator + version, lang + ".lang");
if (!file.exists()) {
Expand All @@ -89,7 +90,7 @@ public void downloadAndApplyLanguage(String version, String lang) {
} catch (IOException | InvalidConfigurationException | IllegalArgumentException ex) {
this.getLogger().log(Level.WARNING, "Failed to download " + file.getName(), ex);
this.getLogger().log(Level.WARNING, "Using en_US language.");
this.impl.keys.clear();
this.keys.clear();
return;
}
}
Expand All @@ -99,11 +100,11 @@ public void downloadAndApplyLanguage(String version, String lang) {
public void loadLanguage(File file) {
Charset charset = Charset.forName("UTF-8");
try (InputStreamReader is = new InputStreamReader(new FileInputStream(file), charset)) {
this.impl.keys.load(is);
this.keys.load(is);
} catch (IOException ex) {
this.getLogger().log(Level.WARNING, "Failed to load " + file.getName(), ex);
this.getLogger().log(Level.WARNING, "Using en_US language.");
this.impl.keys.clear();
this.keys.clear();
}
}
}
112 changes: 91 additions & 21 deletions src/club/without/dereku/itemtooltips/Listeners.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,20 @@
*/
package club.without.dereku.itemtooltips;

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;
import org.bukkit.Material;
import org.bukkit.entity.Item;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.ItemMergeEvent;
import org.bukkit.event.entity.ItemSpawnEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BannerMeta;
import org.bukkit.inventory.meta.ItemMeta;

/**
Expand All @@ -39,40 +46,103 @@
public class Listeners implements Listener {

private final ItemTooltips plugin;
private final String withoutAmount;
private final String withAmount;

public Listeners(ItemTooltips aThis) {
private final Class<?> obcbCraftItemStack;
private final Class<?> nmsItemStack;
private final Method asNMSCopy;
private final Method itemStack_getName;
private final Method itemStack_getI18n;

public Listeners(ItemTooltips aThis) throws ClassNotFoundException, SecurityException, NoSuchMethodException {
this.plugin = aThis;
this.withAmount = ChatColor.translateAlternateColorCodes('&',
this.plugin.getConfig().getString("format.withAmount", "%name%")
);
this.withoutAmount = ChatColor.translateAlternateColorCodes('&',
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);
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);
this.itemStack_getName = this.nmsItemStack.getDeclaredMethod("getName", (Class<?>[]) null);
this.itemStack_getI18n = this.nmsItemStack.getDeclaredMethod("a", (Class<?>[]) null);
}
@EventHandler(ignoreCancelled=true)

@EventHandler(ignoreCancelled = true)
public void onItemSpawnEvent(ItemSpawnEvent event) {
if (!this.plugin.worlds.contains(event.getEntity().getLocation().getWorld().getName())) {
return;
}
this.setName(event.getEntity(), event.getEntity().getItemStack());
}
@EventHandler(ignoreCancelled=true)

@EventHandler(ignoreCancelled = true)
public void onItemMergeEvent(ItemMergeEvent event) {
if (!this.plugin.worlds.contains(event.getTarget().getLocation().getWorld().getName())) {
return;
}
ItemStack is = event.getEntity().getItemStack().clone();
is.setAmount(is.getAmount() + event.getTarget().getItemStack().getAmount());
this.setName(event.getTarget(), is);
}

private void setName(Item item, ItemStack itemStack) {
if (!this.plugin.worlds.contains(item.getLocation().getWorld().getName())) {
return;
}
ItemMeta im = item.getItemStack().getItemMeta();
String name = this.plugin.getConfig().getString("format.withoutAmount", "%name%");
if (itemStack.getAmount() > 1) {
name = this.plugin.getConfig().getString("format.withAmount", "%name% x%amount%");
}

String displayName = im.hasDisplayName() ? im.getDisplayName() : this.plugin.getImpl().getName(item);
item.setCustomName(
ChatColor.translateAlternateColorCodes('&',
name
.replace("%name%", displayName)
.replace("%amount%", Integer.toString(itemStack.getAmount())))
);
String name = itemStack.getAmount() > 1 ? this.withAmount : this.withoutAmount;
String displayName = im.hasDisplayName() ? im.getDisplayName() : this.getName(item);
item.setCustomName(name.replace("%name%", displayName).replace("%amount%", Integer.toString(itemStack.getAmount())));
item.setCustomNameVisible(true);
}

private String getName(Item item) {
String i18n;
try {
Object nmsis = this.asNMSCopy.invoke(null, item.getItemStack());
if (this.plugin.keys.isEmpty()) {
return (String) this.itemStack_getName.invoke(nmsis, (Object) null);
}
i18n = (String) this.itemStack_getI18n.invoke(nmsis, (Object) null);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
this.plugin.getLogger().log(Level.WARNING, "Failed to get name", ex);
return null;
}

String out = this.getBannerKey(item);
if (out == null) {
out = i18n.concat(".name");
}

return this.plugin.keys.getProperty(out, out);
}

public 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(".")
.append(bm.getBaseColor().toString().toLowerCase().replace("light_blue", "lightBlue"))
.append(".name");
} catch (Exception ex) {
out.append(ex.getMessage());
}
return out.toString();
}
}

This file was deleted.

Loading

0 comments on commit acfd976

Please sign in to comment.