| package net.glowstone.inventory; |
| |
| import net.glowstone.entity.GlowPlayer; |
| import net.glowstone.util.InventoryUtil; |
| import org.bukkit.ChatColor; |
| import org.bukkit.Material; |
| import org.bukkit.event.inventory.InventoryType; |
| import org.bukkit.event.inventory.InventoryType.SlotType; |
| import org.bukkit.inventory.AnvilInventory; |
| import org.bukkit.inventory.InventoryHolder; |
| import org.bukkit.inventory.InventoryView; |
| import org.bukkit.inventory.ItemStack; |
| import org.bukkit.inventory.meta.EnchantmentStorageMeta; |
| import org.bukkit.inventory.meta.ItemMeta; |
| import org.bukkit.enchantments.Enchantment; |
| import java.util.Map; |
| import java.util.Objects; |
| |
| public class GlowAnvilInventory extends GlowInventory implements AnvilInventory { |
| |
| private static final int FIRST_ITEM_SLOT = 0; |
| private static final int SECOND_ITEM_SLOT = 1; |
| private static final int RESULT_SLOT = 2; |
| private String rename = ""; |
| private int repairCost; |
| |
| public GlowAnvilInventory(InventoryHolder holder) { |
| super(holder, InventoryType.ANVIL); |
| |
| getSlot(FIRST_ITEM_SLOT).setType(SlotType.CRAFTING); |
| getSlot(SECOND_ITEM_SLOT).setType(SlotType.CRAFTING); |
| getSlot(RESULT_SLOT).setType(SlotType.RESULT); |
| } |
| |
| @Override |
| public int getRawSlots() { |
| return 0; |
| } |
| |
| public ItemStack getFirstItem() { |
| return getSlot(FIRST_ITEM_SLOT).getItem(); |
| } |
| |
| public ItemStack getSecondItem() { |
| return getSlot(SECOND_ITEM_SLOT).getItem(); |
| } |
| |
| public ItemStack getResultItem() { |
| return getSlot(RESULT_SLOT).getItem(); |
| } |
| |
| @Override |
| public void handleShiftClick(GlowPlayer player, InventoryView view, int clickedSlot, ItemStack clickedItem) { |
| if (getSlotType(view.convertSlot(clickedSlot)) == SlotType.RESULT) { |
| // If the player clicked on the result give it to them |
| ItemStack forged = getForged(); |
| if (forged == null) { |
| return; // we can't smith, my liege |
| } |
| |
| // Smith the item |
| smith(); |
| |
| // Place the item in the player's inventory (right to left) |
| player.getInventory().tryToFillSlots(clickedItem, 8, -1, 35, 8); |
| } else { |
| // Clicked in the crafting grid, no special handling required (just place them left to right) |
| clickedItem = player.getInventory().tryToFillSlots(clickedItem, 9, 36, 0, 9); |
| view.setItem(clickedSlot, clickedItem); |
| } |
| } |
| |
| private void smith() { |
| setItem(FIRST_ITEM_SLOT, InventoryUtil.createEmptyStack()); |
| setItem(SECOND_ITEM_SLOT, InventoryUtil.createEmptyStack()); |
| } |
| |
| @Override |
| public void setItem(int index, ItemStack item) { |
| super.setItem(index, item); |
| |
| if (index != RESULT_SLOT) { |
| ItemStack forged = getForged(); |
| if (forged == null) { |
| super.setItem(RESULT_SLOT, InventoryUtil.createEmptyStack()); |
| } else { |
| super.setItem(RESULT_SLOT, forged); |
| } |
| } |
| } |
| |
| public ItemStack getForged() { |
| if (InventoryUtil.isEmpty(getFirstItem()) || InventoryUtil.isEmpty(getSecondItem())) { |
| return null; |
| } |
| if (getSecondItem().getType() == Material.ENCHANTED_BOOK) { |
| EnchantmentStorageMeta book = (EnchantmentStorageMeta) getSecondItem().getItemMeta(); |
| ItemStack result; |
| if (InventoryUtil.isEmpty(getResultItem())) { |
| result = getFirstItem().clone(); |
| } else { |
| result = getResultItem(); |
| } |
| for(Map.Entry<Enchantment, Integer> entry : book.getStoredEnchants().entrySet()) { |
| Enchantment enchantment = entry.getKey(); |
| if(!enchantment.canEnchantItem(result)) continue; |
| if(result.getType() != Material.ENCHANTED_BOOK) continue; |
| result.addUnsafeEnchantment(enchantment, entry.getValue().intValue()); |
| } |
| return result; |
| } |
| return null; |
| } |
| |
| @Override |
| public String getRenameText() { |
| return rename; |
| } |
| |
| public void setRenameText(String name) { |
| rename = name; |
| if (rename.isEmpty()) { |
| setItem(FIRST_ITEM_SLOT, getFirstItem()); |
| setItem(SECOND_ITEM_SLOT, getSecondItem()); |
| } else { |
| ItemStack result = getFirstItem().clone(); |
| if (!InventoryUtil.isEmpty(result)) { |
| if (Objects.equals(result.getItemMeta().getDisplayName(), name)) { |
| setItem(RESULT_SLOT, InventoryUtil.createEmptyStack()); |
| } |
| // rename the item |
| ItemMeta m = result.getItemMeta(); |
| m.setDisplayName(ChatColor.ITALIC + rename); |
| result.setItemMeta(m); |
| setItem(RESULT_SLOT, result); |
| } |
| } |
| } |
| |
| @Override |
| public int getRepairCost() { |
| return repairCost; |
| } |
| |
| @Override |
| public void setRepairCost(int levels) { |
| repairCost = levels; |
| } |
| } |