@SubCommand(value = "info", permission = "nu.repair") public void repairInfo(CommandSender sender, Arguments args) { ItemStack item = getItemInHand(sender); RepairInstance info = new RepairInstance(item, plugin.cfg.repair, plugin); new Message(I18n.format("user.repair.info_1")).append(item).send(asPlayer(sender)); msg(sender, "user.repair.info_2", item.getType().name()); if (info.stat != REPAIRABLE) { msg(sender, "user.repair.info_3", I18n.format("user.repair.unrepairable." + info.stat.name())); } if (info.stat == UNREPAIRABLE) return; int fullDur = item.getType().getMaxDurability(); int currDur = fullDur - item.getDurability(); msg(sender, "user.repair.info_4", currDur, fullDur, (double) currDur / (double) fullDur * 100); new Message(I18n.format("user.repair.info_5")).append(new ItemStack(info.repairMaterial)).send(asPlayer(sender)); msg(sender, "user.repair.info_6", info.expConsumption); msg(sender, "user.repair.info_7", info.durRecovered, (double) info.durRecovered / (double) fullDur * 100); if (info.repairLimit <= 0) { msg(sender, "user.repair.info_8"); } else { int repairTime = ((Repairable) item.getItemMeta()).getRepairCost(); msg(sender, "user.repair.info_9", repairTime, info.repairLimit); } if (info.stat == REPAIRABLE) { msg(sender, "user.repair.info_10", (int) Math.ceil(item.getDurability() / (double) info.durRecovered)); } }
private void increaseReapirCount(ItemStack item, int x) { if (x == 0) return; ItemMeta meta = item.getItemMeta(); if (meta instanceof Repairable) { Repairable r = (Repairable) meta; int count = r.getRepairCost() + x; if (count < 0) count = 0; r.setRepairCost(count); item.setItemMeta(meta); } }
public RepairInstance(ItemStack item, RepairConfig config, NyaaUtils plugin) { if (item == null || item.getType() == Material.AIR) return; RepairConfig.RepairConfigItem cfg = config.getRepairConfig(item.getType()); if (cfg == null) return; if (!(item.getItemMeta() instanceof Repairable)) return; if (item.hasItemMeta() && item.getItemMeta().hasLore()) { if (!plugin.cfg.globalLoreBlacklist.canRepair(item.getItemMeta().getLore())) { stat = RepairStat.UNREPAIRABLE; return; } } stat = RepairStat.REPAIRABLE; if (item.getItemMeta().isUnbreakable()) { stat = RepairStat.UNREPAIRABLE_UNBREAKABLE; } Repairable repairableMeta = (Repairable) item.getItemMeta(); repairLimit = cfg.repairLimit; if (repairLimit > 0 && repairableMeta.getRepairCost() >= repairLimit) { stat = RepairStat.UNREPAIRABLE_RLE; } Material toolMaterial = item.getType(); repairMaterial = cfg.material; int currentDurability = item.getDurability(); if (currentDurability <= 0) { stat = RepairStat.UNREPAIRABLE_REPAIRED; } int enchLevel = 0; for (Integer i : item.getEnchantments().values()) enchLevel += i; int fullDurability = toolMaterial.getMaxDurability(); durRecovered = (int) Math.floor((double) fullDurability / ((double) cfg.fullRepairCost + (double) enchLevel * cfg.enchantCostPerLv)); expConsumption = (int) Math.floor(cfg.expCost + cfg.enchantCostPerLv * enchLevel); if (durRecovered <= 0) { stat = RepairStat.UNREPAIRABLE_LOWRECOVER; } }
/** * Sets this item's repair cost. * * @param cost repair cost * * @return this item builder instance, for chaining */ public ItemBuilder repairCost(int cost) { try { ((Repairable) this.itemMeta).setRepairCost(cost); } catch (Exception e) { if (!this.failSilently) { e.printStackTrace(); } } return this; }
public boolean matches(ItemStack anotherItem) { ItemStack base = itemTemplate.clone(); ItemStack given = anotherItem.clone(); base.setAmount(1); given.setAmount(1); if (requireExact) return base.equals(given); if (!base.getType().equals(given.getType())) return false; if (repairCostMatch == MatchingMode.EXACT && base.getItemMeta() instanceof Repairable && given.getItemMeta() instanceof Repairable && !(((Repairable) given.getItemMeta()).getRepairCost() == ((Repairable) base.getItemMeta()).getRepairCost())) { return false; } int baseDamage = base.getDurability(); int givenDamage = given.getDurability(); if (minDamageValue == -2 && givenDamage < baseDamage) return false; if (minDamageValue >= 0 && givenDamage < minDamageValue) return false; if (maxDamageValue == -2 && givenDamage > baseDamage) return false; if (maxDamageValue >= 0 && givenDamage > maxDamageValue) return false; String baseDisplay = getDisplayName(base); String givenDisplay = getDisplayName(given); if (nameMatch == MatchingMode.EXACT && !baseDisplay.equals(givenDisplay)) return false; if (nameMatch == MatchingMode.EXACT_TEXT && !ChatColor.stripColor(baseDisplay).equals(ChatColor.stripColor(givenDisplay))) return false; if (nameMatch == MatchingMode.CONTAINS && !givenDisplay.contains(baseDisplay)) return false; if (nameMatch == MatchingMode.CONTAINS_TEXT && !ChatColor.stripColor(givenDisplay).contains(ChatColor.stripColor(baseDisplay))) return false; Map<Enchantment, Integer> baseEnch = base.getEnchantments(); Map<Enchantment, Integer> givenEnch = given.getEnchantments(); if (enchantMatch == MatchingMode.EXACT || enchantMatch == MatchingMode.EXACT_TEXT) { if (!baseEnch.equals(givenEnch)) return false; } else if (enchantMatch == MatchingMode.CONTAINS || enchantMatch == MatchingMode.CONTAINS_TEXT) { for (Map.Entry<Enchantment, Integer> e : baseEnch.entrySet()) { if (!givenEnch.containsKey(e.getKey()) || givenEnch.get(e.getKey()) < e.getValue()) return false; } } String[] baseLore = getLore(base); String[] givenLore = getLore(given); if (loreMatch == MatchingMode.EXACT && !Arrays.deepEquals(baseLore, givenLore)) return false; if (loreMatch == MatchingMode.CONTAINS && !containStrArr(givenLore, baseLore, false)) return false; if (loreMatch == MatchingMode.EXACT_TEXT) { for (int i = 0; i < baseLore.length; i++) baseLore[i] = ChatColor.stripColor(baseLore[i]); for (int i = 0; i < givenLore.length; i++) givenLore[i] = ChatColor.stripColor(givenLore[i]); if (!Arrays.deepEquals(baseLore, givenLore)) return false; } if (loreMatch == MatchingMode.CONTAINS_TEXT && !containStrArr(givenLore, baseLore, true)) return false; return true; }
public boolean matches(ItemStack anotherItem) { ItemStack base = itemTemplate.clone(); ItemStack given = anotherItem.clone(); base.setAmount(1); given.setAmount(1); if (requireExact) return base.equals(given); if (!base.getType().equals(given.getType())) return false; if (repairCostMatch == MatchingMode.EXACT) { if (base.getItemMeta() instanceof Repairable && given.getItemMeta() instanceof Repairable) { int cost1 = ((Repairable) given.getItemMeta()).getRepairCost(); int cost2 = ((Repairable) base.getItemMeta()).getRepairCost(); if (cost1 != cost2) return false; } else if (base.getItemMeta() instanceof Repairable || given.getItemMeta() instanceof Repairable) { return false; } } int baseDamage = base.getDurability(); int givenDamage = given.getDurability(); if (minDamageValue == -2 && givenDamage < baseDamage) return false; if (minDamageValue >= 0 && givenDamage < minDamageValue) return false; if (maxDamageValue == -2 && givenDamage > baseDamage) return false; if (maxDamageValue >= 0 && givenDamage > maxDamageValue) return false; String baseDisplay = getDisplayName(base); String givenDisplay = getDisplayName(given); if (nameMatch == MatchingMode.EXACT && !baseDisplay.equals(givenDisplay)) return false; if (nameMatch == MatchingMode.EXACT_TEXT && !ChatColor.stripColor(baseDisplay).equals(ChatColor.stripColor(givenDisplay))) return false; if (nameMatch == MatchingMode.CONTAINS && !givenDisplay.contains(baseDisplay)) return false; if (nameMatch == MatchingMode.CONTAINS_TEXT && !ChatColor.stripColor(givenDisplay).contains(ChatColor.stripColor(baseDisplay))) return false; Map<Enchantment, Integer> baseEnch = base.getEnchantments(); Map<Enchantment, Integer> givenEnch = given.getEnchantments(); if (enchantMatch == MatchingMode.EXACT || enchantMatch == MatchingMode.EXACT_TEXT) { if (!baseEnch.equals(givenEnch)) return false; } else if (enchantMatch == MatchingMode.CONTAINS || enchantMatch == MatchingMode.CONTAINS_TEXT) { for (Map.Entry<Enchantment, Integer> e : baseEnch.entrySet()) { if (!givenEnch.containsKey(e.getKey()) || givenEnch.get(e.getKey()) < e.getValue()) return false; } } String[] baseLore = getLore(base); String[] givenLore = getLore(given); if (loreMatch == MatchingMode.EXACT && !Arrays.deepEquals(baseLore, givenLore)) return false; if (loreMatch == MatchingMode.CONTAINS && !containStrArr(givenLore, baseLore, false)) return false; if (loreMatch == MatchingMode.EXACT_TEXT) { for (int i = 0; i < baseLore.length; i++) baseLore[i] = ChatColor.stripColor(baseLore[0]); for (int i = 0; i < baseLore.length; i++) baseLore[i] = ChatColor.stripColor(baseLore[0]); if (!Arrays.deepEquals(baseLore, givenLore)) return false; } if (loreMatch == MatchingMode.CONTAINS_TEXT && !containStrArr(givenLore, baseLore, true)) return false; return true; }
@Override public boolean apply(@Nullable ItemStack item1, @Nullable ItemStack item2) { Boolean b = precondition(item1, item2, true); return b != null ? b : ((Repairable) item1.getItemMeta()).getRepairCost() == ((Repairable) item2.getItemMeta()).getRepairCost(); }
/** * initModule enables the module called by {@link TerraCraftTools} */ public void initModule(TerraCraftTools sst) { plugin = sst; kits = new HashMap<String, List<ItemStack>>(); cooldowns = new HashMap<Player, Long>(); File[] kitFiles = plugin.getDataFolder().listFiles(new IsYML()); if (kitFiles.length == 0) { return; } //This is prone to throw some exceptions if the config is poorly written. //If you feel like cleaning it up, feel free to Pull Request. for (File kit : kitFiles) { FileConfiguration kitConf = YamlConfiguration.loadConfiguration(kit); List<String> desc = kitConf.getStringList("description"); List<ItemStack> items = new LinkedList<ItemStack>(); List<String> itemConfs = kitConf.getStringList("items"); for (String itemConf : itemConfs) { ItemStack itemStack = null; String[] bits = itemConf.split(" "); for (String bit : bits) { if (bit.startsWith("id:")) { itemStack = new ItemStack(Integer.valueOf(bit.substring(3)), 1); } if (itemStack == null) { continue; } if (bit.startsWith("count:")) { itemStack.setAmount(Integer.valueOf(bit.substring(6))); } else if (bit.startsWith("!tagged")) { ItemMeta im = itemStack.getItemMeta(); im.setLore(desc); itemStack.setItemMeta(im); } else if (bit.startsWith("data:")) { itemStack.setDurability(Short.valueOf(bit.substring(5))); } else if (bit.startsWith("ench:")) { String ench = bit.substring(5); String[] bites = ench.split(":"); //I don't want to deal with cosmetic names. itemStack.addUnsafeEnchantment(Enchantment.getByName(bites[0]), Integer.valueOf(bites[1])); } else if (bit.startsWith("anvilCost:")) { Repairable r = (Repairable) itemStack.getItemMeta(); r.setRepairCost(Integer.valueOf(bit.substring(10))); itemStack.setItemMeta((ItemMeta) r); } } items.add(itemStack); } kits.put(kitConf.getString("kitName").toLowerCase(), items); } plugin.commandRegistrar.registerCommand("kit", this); plugin.commandRegistrar.registerCommand("identify", this); plugin.logger.info("[TerraCraftTools][GuardKits] GuardKits Module Enabled!"); }
@EventHandler public static void onInventoryClick(InventoryClickEvent e){ if(e.isCancelled()) return; HumanEntity ent = e.getWhoClicked(); if(!(ent instanceof Player)) return; Player player = (Player)ent; Inventory inv = e.getInventory(); if(!(inv instanceof AnvilInventory)) return; EpicPlayer epicPlayer = EpicSystem.getEpicPlayer(player.getUniqueId()); List<EpicQuestTask> taskList = epicPlayer.getTasksByType(TaskTypes.REPAIR_ITEM); for(EpicQuestTask task : taskList){ AnvilInventory anvil = (AnvilInventory)inv; InventoryView view = e.getView(); int rawSlot = e.getRawSlot(); if(rawSlot == view.convertSlot(rawSlot)){ // 2 = result slot if(rawSlot != 2) return; ItemStack[] items = anvil.getContents(); ItemStack item1 = items[0]; ItemStack item3 = e.getCurrentItem(); //if(item1 == null || item2 == null || item3 == null) return; Material id1 = item1.getType(); Material id3 = item3.getType(); if(id1 != id3) return; //See if correct item is repaired if(id3 == Material.matchMaterial(task.getTaskID())){ ItemMeta meta = item3.getItemMeta(); if(meta == null) return; if(meta instanceof Repairable){ Repairable repairable = (Repairable)meta; int repairCost = repairable.getRepairCost(); if(player.getLevel() >= repairCost){ task.ProgressTask(1, epicPlayer, true); } } } } } }
/** * Instantiates an ItemStack with a display name, lore, enchantments, and a cost for repairing. * * @param type material * @param amount amount * @param durability damage / durability * @param displayName name of item * @param lore lore for item * @param enchantments enchantments for item * @param cost experience level cost to repair */ public NonrepairableItemStack(Material type, int amount, short durability, String displayName, List<String> lore, Map<Enchantment, Integer> enchantments, int cost) { super(type, amount, durability, displayName, lore, enchantments); if (this.getItemMeta() instanceof Repairable) { Repairable repairable = (Repairable) this.getItemMeta(); repairable.setRepairCost(cost); setItemMeta((ItemMeta) repairable); } }
Repairable clone();