diff --git a/src/pw/chew/transmuteit/TransmuteGUI.java b/src/pw/chew/transmuteit/TransmuteGUI.java index ac94975..5bb3e5e 100644 --- a/src/pw/chew/transmuteit/TransmuteGUI.java +++ b/src/pw/chew/transmuteit/TransmuteGUI.java @@ -41,6 +41,7 @@ public void initializeItems(UUID uuid, String args[], Player player) { inv.setItem(10, createGuiItem(Material.getMaterial("PLAYER_HEAD"), "You", ChatColor.YELLOW + "EMC: " + ChatColor.GREEN + NumberFormat.getInstance().format(emc), ChatColor.YELLOW + "Discoveries: " + ChatColor.GREEN + discoveries + " / " + totalDiscoveries)); inv.setItem(12, createGuiItem(Material.getMaterial("PAPER"), "Help!", "Click to view help!")); inv.setItem(14, createGuiItem(Material.getMaterial("ENCHANTING_TABLE"), "Discoveries", "" + "Click to view your discoveries.")); + inv.setItem(16, createGuiItem(Material.getMaterial("BUCKET"), "Transmute Take", "" + "Turn items INTO EMC from your inventory!")); } // Nice little method to create a gui item with a custom name, and description @@ -98,6 +99,12 @@ public void onInventoryClick(InventoryClickEvent e) { gui.openInventory(player); } + if(e.getRawSlot() == 16) { + TransmuteTakeGUI gui = new TransmuteTakeGUI(); + gui.initializeItems(player.getUniqueId(), player); + gui.openInventory(player); + } + } diff --git a/src/pw/chew/transmuteit/TransmuteIt.java b/src/pw/chew/transmuteit/TransmuteIt.java index 7aa6f4c..a1b1d20 100644 --- a/src/pw/chew/transmuteit/TransmuteIt.java +++ b/src/pw/chew/transmuteit/TransmuteIt.java @@ -82,6 +82,7 @@ public void onEnable() { // Register Events getServer().getPluginManager().registerEvents(new DiscoveriesGUI(), this); getServer().getPluginManager().registerEvents(new TransmuteGUI(), this); + getServer().getPluginManager().registerEvents(new TransmuteTakeGUI(), this); // Magic Time getLogger().info("Booted!"); diff --git a/src/pw/chew/transmuteit/TransmuteTakeGUI.java b/src/pw/chew/transmuteit/TransmuteTakeGUI.java new file mode 100644 index 0000000..7cc6294 --- /dev/null +++ b/src/pw/chew/transmuteit/TransmuteTakeGUI.java @@ -0,0 +1,132 @@ +package pw.chew.transmuteit; + +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.Material; +import org.bukkit.entity.HumanEntity; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.inventory.ClickType; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryHolder; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.PlayerInventory; +import org.bukkit.inventory.meta.Damageable; +import org.bukkit.inventory.meta.ItemMeta; + +import java.text.NumberFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.UUID; + +public class TransmuteTakeGUI implements InventoryHolder, Listener { + private Inventory inv; + + public TransmuteTakeGUI() { + inv = Bukkit.createInventory(this, 9, "Click Items to Transmute"); + } + + @Override + public Inventory getInventory() { + return inv; + } + + // You can call this whenever you want to put the items in + public void initializeItems(UUID uuid, Player player) { + for(int i=0; i < 9; i++) { + inv.setItem(i, createGuiItem(Material.getMaterial("GRAY_STAINED_GLASS_PANE"), "")); + } + } + + // Nice little method to create a gui item with a custom name, and description + private ItemStack createGuiItem(Material material, String name, String...lore) { + ItemStack item = new ItemStack(material, 1); + ItemMeta meta = item.getItemMeta(); + meta.setDisplayName(ChatColor.RESET + name); + + ArrayList metaLore = new ArrayList(Arrays.asList(lore)); + + meta.setLore(metaLore); + item.setItemMeta(meta); + return item; + } + + // You can open the inventory with this + public void openInventory(Player p) { + p.openInventory(inv); + } + + // Check for clicks on items + @EventHandler + public void onInventoryClick(InventoryClickEvent e) { + if (e.getInventory().getHolder().getClass() != this.getClass()) { + return; + } + if (e.getClick().equals(ClickType.NUMBER_KEY)){ + e.setCancelled(true); + } + e.setCancelled(true); + + Player player = (Player) e.getWhoClicked(); + ItemStack clickedItem = e.getCurrentItem(); + PlayerInventory inventory = player.getInventory(); + + // verify current item is not null + if (clickedItem == null || clickedItem.getType() == Material.AIR) return; + + int selectedItem = e.getRawSlot(); + if(selectedItem > 8) { + if(selectedItem >= 36) { + selectedItem -= 36; + } + ItemStack item = inventory.getItem(selectedItem); + int amount = item.getAmount(); + + Material type = item.getType(); + String name = type.toString(); + // If it's nothing + // If it's something + try { + DataManager bob = new DataManager(); + int emc = TransmuteIt.json.getInt(type.toString()); + int currentDurability = 0; + ItemMeta meta = item.getItemMeta(); + Damageable damage; + if(meta instanceof Damageable) { + damage = ((Damageable) meta); + currentDurability = damage.getDamage(); + } + short maxDurability = type.getMaxDurability(); + if(currentDurability > maxDurability) { + currentDurability = maxDurability; + } + if(maxDurability > 0) { + emc = (int)((double)emc * (((double)maxDurability-(double)currentDurability)/(double)maxDurability)); + } + item.setAmount(0); + UUID uuid = player.getUniqueId(); + int current = new DataManager().getEMC(uuid, player); + int newEMC = current + (amount * emc); + bob.writeEMC(uuid, newEMC, player); + player.sendMessage(ChatColor.COLOR_CHAR + "d--------[ " + ChatColor.COLOR_CHAR + "bTransmuting Stats" + ChatColor.COLOR_CHAR + "d ]--------"); + if(!bob.discovered(uuid, name)) { + player.sendMessage(ChatColor.COLOR_CHAR + "aYou've discovered " + name + "!"); + if(bob.discoveries(uuid).size() == 0) { + player.sendMessage(ChatColor.ITALIC + "" + ChatColor.COLOR_CHAR + "7Now you can run /transmute get " + name + " [amount] to get this item, given you have enough EMC!"); + } + new DataManager().writeDiscovery(uuid, name); + } + player.sendMessage(ChatColor.GREEN + "+ " + NumberFormat.getInstance().format(amount * emc) + " EMC [Total: " + NumberFormat.getInstance().format(newEMC) + " EMC]"); + player.sendMessage(ChatColor.RED + "- " + amount + " " + new DiscoveriesGUI().capitalize(name)); + return; + // If there's no JSON file or it's not IN the JSON file + } catch(org.json.JSONException f) { + player.sendMessage("This item has no set EMC value!"); + } + } + + } +}