org.bukkit.inventory.meta.Damageable Java Examples
The following examples show how to use
org.bukkit.inventory.meta.Damageable.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Power.java From MineTinker with GNU General Public License v3.0 | 6 votes |
private void powerCreateFarmland(Player player, ItemStack tool, Block block) { if (block.getType().equals(Material.GRASS_BLOCK) || block.getType().equals(Material.DIRT)) { if (block.getWorld().getBlockAt(block.getLocation().add(0, 1, 0)).getType().equals(Material.AIR)) { if (tool.getItemMeta() instanceof Damageable) { Damageable damageable = (Damageable) tool.getItemMeta(); damageable.setDamage(damageable.getDamage() + 1); tool.setItemMeta((ItemMeta) damageable); } PlayerInteractEvent event = new PlayerInteractEvent(player, Action.RIGHT_CLICK_BLOCK, tool, block, BlockFace.UP); Bukkit.getPluginManager().callEvent(event); block.setType(Material.FARMLAND); // Event only does Plugin event (no vanilla conversion to Farmland and // Tool-Damage) } } }
Example #2
Source File: DamageableItem.java From Slimefun4 with GNU General Public License v3.0 | 6 votes |
/** * This method will damage the given {@link ItemStack} once. * It also takes into account the {@link Enchantment} {@code Unbreaking}. * * It will only apply the damage if {@link #isDamageable()} returned true. * * @param p * The {@link Player} to which the item belongs * @param item * The {@link ItemStack} to damage */ default void damageItem(Player p, ItemStack item) { if (isDamageable() && item != null && item.getType() != Material.AIR && item.getAmount() > 0) { if (item.getEnchantments().containsKey(Enchantment.DURABILITY) && Math.random() * 100 <= (60 + Math.floorDiv(40, (item.getEnchantmentLevel(Enchantment.DURABILITY) + 1)))) { return; } ItemMeta meta = item.getItemMeta(); Damageable damageable = (Damageable) meta; if (damageable.getDamage() >= item.getType().getMaxDurability()) { item.setAmount(0); } else { damageable.setDamage(damageable.getDamage() + 1); item.setItemMeta(meta); } } }
Example #3
Source File: ModManager.java From MineTinker with GNU General Public License v3.0 | 6 votes |
/** * Checks the durability of the Tool * * @param cancellable the Event (implements Cancelable) * @param player the Player * @param tool the Tool * @return false: if broken; true: if enough durability */ public boolean durabilityCheck(@NotNull Cancellable cancellable, @NotNull Player player, @NotNull ItemStack tool) { ItemMeta meta = tool.getItemMeta(); if (meta instanceof Damageable) { if (config.getBoolean("UnbreakableTools", true) && tool.getType().getMaxDurability() - ((Damageable) meta).getDamage() <= 2) { cancellable.setCancelled(true); if (config.getBoolean("Sound.OnBreaking", true)) { player.playSound(player.getLocation(), Sound.ENTITY_ITEM_BREAK, 0.5F, 0.5F); } return false; } } return true; }
Example #4
Source File: HashedArmorpiece.java From Slimefun4 with GNU General Public License v3.0 | 6 votes |
/** * This will update this {@link HashedArmorpiece} with the given {@link ItemStack} * and the corresponding {@link SlimefunItem} * * @param stack * The new armorpiece to be stored in this {@link HashedArmorpiece} * @param item * The {@link SlimefunItem} corresponding to the provided {@link ItemStack}, may be null */ public void update(ItemStack stack, SlimefunItem item) { if (stack == null || stack.getType() == Material.AIR) { this.hash = 0; } else { ItemStack copy = stack.clone(); ItemMeta meta = copy.getItemMeta(); ((Damageable) meta).setDamage(0); copy.setItemMeta(meta); this.hash = copy.hashCode(); } if (item instanceof SlimefunArmorPiece) { this.item = Optional.of((SlimefunArmorPiece) item); } else { this.item = Optional.empty(); } }
Example #5
Source File: UtilsMc.java From NBTEditor with GNU General Public License v3.0 | 5 votes |
public static boolean offsetItemStackDamage(ItemStack item, int offset) { ItemMeta meta = item.getItemMeta(); if (meta instanceof Damageable) { ((Damageable) meta).setDamage(((Damageable) meta).getDamage() + offset); item.setItemMeta(meta); return true; } return false; }
Example #6
Source File: AutoAnvil.java From Slimefun4 with GNU General Public License v3.0 | 5 votes |
private ItemStack repair(ItemStack item) { ItemStack repaired = item.clone(); ItemMeta meta = repaired.getItemMeta(); short maxDurability = item.getType().getMaxDurability(); short durability = (short) (((Damageable) meta).getDamage() - (maxDurability / getRepairFactor())); if (durability < 0) { durability = 0; } ((Damageable) meta).setDamage(durability); repaired.setItemMeta(meta); return repaired; }
Example #7
Source File: TalismanListener.java From Slimefun4 with GNU General Public License v3.0 | 5 votes |
@EventHandler public void onItemBreak(PlayerItemBreakEvent e) { if (Talisman.checkFor(e, SlimefunItems.TALISMAN_ANVIL)) { PlayerInventory inv = e.getPlayer().getInventory(); int slot = inv.getHeldItemSlot(); // Did the tool in our hand broke or was it an Armorpiece? if (!inv.getItem(inv.getHeldItemSlot()).equals(e.getBrokenItem())) { for (int s : armorSlots) { if (e.getBrokenItem().equals(inv.getItem(s))) { slot = s; break; } } } ItemStack item = e.getBrokenItem().clone(); ItemMeta meta = item.getItemMeta(); if (meta instanceof Damageable) { ((Damageable) meta).setDamage(0); } item.setItemMeta(meta); int itemSlot = slot; // Update the item forcefully Slimefun.runSync(() -> inv.setItem(itemSlot, item), 1L); } }
Example #8
Source File: ChestMenuUtils.java From Slimefun4 with GNU General Public License v3.0 | 5 votes |
public static void updateProgressbar(ChestMenu menu, int slot, int timeLeft, int time, ItemStack indicator) { ItemStack item = indicator.clone(); ItemMeta im = item.getItemMeta(); im.addItemFlags(ItemFlag.HIDE_ATTRIBUTES); if (im instanceof Damageable) { ((Damageable) im).setDamage(getDurability(item, timeLeft, time)); } im.setDisplayName(" "); im.setLore(Arrays.asList(getProgressBar(timeLeft, time), "", ChatColor.GRAY + NumberUtils.getTimeLeft(timeLeft / 2) + " left")); item.setItemMeta(im); menu.replaceExistingItem(slot, item); }
Example #9
Source File: HashedArmorpiece.java From Slimefun4 with GNU General Public License v3.0 | 5 votes |
/** * This method checks whether the given {@link ItemStack} is no longer similar to the * one represented by this {@link HashedArmorpiece}. * * @param stack * The {@link ItemStack} to compare * @return Whether the {@link HashedArmorpiece} and the given {@link ItemStack} mismatch */ public boolean hasDiverged(ItemStack stack) { if (stack == null || stack.getType() == Material.AIR) { return hash == 0; } else { ItemStack copy = stack.clone(); ItemMeta meta = copy.getItemMeta(); ((Damageable) meta).setDamage(0); copy.setItemMeta(meta); return copy.hashCode() != hash; } }
Example #10
Source File: RepairCommands.java From NyaaUtils with MIT License | 5 votes |
@SubCommand(value = "hand", permission = "nu.repair") public void repairHand(CommandSender sender, Arguments args) { ItemStack item = getItemInHand(sender); ItemMeta itemMeta = item.getItemMeta(); ItemStack material = getItemInOffHand(sender); RepairInstance info = new RepairInstance(item, plugin.cfg.repair, plugin); if (info.stat != REPAIRABLE) { msg(sender, "user.repair.info_3", I18n.format("user.repair.unrepairable." + info.stat.name())); return; } if (material.getType() != info.repairMaterial || material.getAmount() <= 0) { msg(sender, "user.repair.material_mismatch"); return; } Player p = asPlayer(sender); if (ExperienceUtils.getExpPoints(p) < info.expConsumption) { msg(sender, "user.repair.no_enough_exp"); return; } ExperienceUtils.subtractExpPoints(p, info.expConsumption); int dur = ((Damageable) itemMeta).getDamage(); dur -= info.durRecovered; if (dur < 0) dur = 0; ((Damageable) itemMeta).setDamage(dur); item.setItemMeta(itemMeta); increaseReapirCount(item, 1); p.getInventory().setItemInMainHand(item); int count = material.getAmount(); if (count <= 1) { p.getInventory().setItemInOffHand(new ItemStack(Material.AIR)); } else { material.setAmount(count - 1); p.getInventory().setItemInOffHand(material); } msg(p, "user.repair.repaired"); }
Example #11
Source File: RepairCommands.java From NyaaUtils with MIT License | 5 votes |
@SubCommand(value = "info", permission = "nu.repair") public void repairInfo(CommandSender sender, Arguments args) { ItemStack item = getItemInHand(sender); ItemMeta itemMeta = item.getItemMeta(); 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 - ((Damageable) itemMeta).getDamage(); 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) itemMeta).getRepairCost(); msg(sender, "user.repair.info_9", repairTime, info.repairLimit); } if (info.stat == REPAIRABLE) { msg(sender, "user.repair.info_10", (int) Math.ceil(((Damageable) itemMeta).getDamage() / (double) info.durRecovered)); } }
Example #12
Source File: LootProtectListener.java From NyaaUtils with MIT License | 5 votes |
private static int compareByDamagePercentage(ItemStack a, ItemStack b) { float delta = (float) ((Damageable) a.getItemMeta()).getDamage() / a.getType().getMaxDurability() - (float) ((Damageable) b.getItemMeta()).getDamage() / b.getType().getMaxDurability(); delta = -delta; if (delta > 0) return 1; if (delta < 0) return -1; return 0; }
Example #13
Source File: LootProtectListener.java From NyaaUtils with MIT License | 5 votes |
private static void giveExp(Player p, int amount) { if (amount <= 0) return; List<ItemStack> candidate = new ArrayList<>(13); for (ItemStack item : new ItemStack[]{ p.getInventory().getHelmet(), p.getInventory().getChestplate(), p.getInventory().getLeggings(), p.getInventory().getBoots(), p.getInventory().getItemInMainHand(), p.getInventory().getItemInOffHand()}) { if (item != null && item.hasItemMeta() && item.getItemMeta().hasEnchant(Enchantment.MENDING)) { if (item.getType().getMaxDurability() > 0 && ((Damageable) item.getItemMeta()).getDamage() > 0) { candidate.add(item); } } } ItemStack repair = null; if (candidate.size() > 0) { candidate.sort(LootProtectListener::compareByDamagePercentage); repair = candidate.get(0); } if (repair != null) { Damageable itemMeta = (Damageable) repair.getItemMeta(); int durability = itemMeta.getDamage(); int repairPoint = durability; if (amount * 2 < repairPoint) repairPoint = amount * 2; int expConsumption = ((repairPoint % 2) == 1) ? (repairPoint + 1) / 2 : repairPoint / 2; itemMeta.setDamage(durability - repairPoint); repair.setItemMeta((ItemMeta) itemMeta); amount -= expConsumption; } if (amount > 0) p.giveExp(amount); PlayerExpChangeEvent event = new PlayerExpChangeEvent(p, amount); Bukkit.getServer().getPluginManager().callEvent(event); }
Example #14
Source File: ItemUtils.java From Skript with GNU General Public License v3.0 | 5 votes |
/** * Sets damage/durability of an item if possible. * @param stack Item to modify. * @param damage New damage. Note that on some Minecraft versions, * this might be truncated to short. */ @SuppressWarnings("deprecation") public static void setDamage(ItemStack stack, int damage) { if (damageMeta) { ItemMeta meta = stack.getItemMeta(); if (meta instanceof Damageable) { ((Damageable) meta).setDamage(damage); stack.setItemMeta(meta); } } else { stack.setDurability((short) damage); } }
Example #15
Source File: ItemUtils.java From Skript with GNU General Public License v3.0 | 5 votes |
/** * Gets damage/durability of an item, or 0 if it does not have damage. * @param stack Item. * @return Damage. */ @SuppressWarnings("deprecation") public static int getDamage(ItemStack stack) { if (damageMeta) { ItemMeta meta = stack.getItemMeta(); if (meta instanceof Damageable) return ((Damageable) meta).getDamage(); return 0; // Not damageable item } else { return stack.getDurability(); } }
Example #16
Source File: ItemStackImpl.java From Civs with GNU General Public License v3.0 | 5 votes |
/** @deprecated */ @Deprecated public void setDurability(short durability) { ItemMeta meta = this.getItemMeta(); if (meta != null) { ((Damageable)meta).setDamage(durability); this.setItemMeta(meta); } }
Example #17
Source File: RepairEffect.java From Civs with GNU General Public License v3.0 | 5 votes |
private void repairItem(PlayerInteractEvent event, Player player, ItemStack item) { ItemMeta itemMeta = item.getItemMeta(); Damageable damageable = (Damageable) itemMeta; if (damageable == null) { player.sendMessage(Civs.getPrefix() + LocaleManager.getInstance().getTranslationWithPlaceholders(player, "cant-repair-item")); event.setCancelled(true); return; } int repairCost = getRepairCost(item.getType(), damageable.getDamage()); if (repairCost == 0) { player.sendMessage(Civs.getPrefix() + LocaleManager.getInstance().getTranslationWithPlaceholders(player, "cant-repair-item")); event.setCancelled(true); return; } HashSet<Material> reagents = getRequiredReagent(item.getType()); if (!reagents.isEmpty()) { boolean hasReagent = false; Material firstMat = reagents.iterator().next(); for (Material mat : reagents) { ItemStack cost = new ItemStack(mat, repairCost); if (!hasReagentCost(player, cost)) { continue; } hasReagent = true; player.getInventory().removeItem(cost); } if (!hasReagent) { String message = LocaleManager.getInstance().getTranslationWithPlaceholders(player, "more-repair-items"); message = message.replace("$1", firstMat.name().toLowerCase().replace("_", " ")); player.sendMessage(Civs.getPrefix() + message); return; } } damageable.setDamage(0); item.setItemMeta(itemMeta); }
Example #18
Source File: ItemService.java From Transport-Pipes with MIT License | 5 votes |
public ItemStack createModelledItem(int damage) { ItemStack woodenPickage = new ItemStack(Material.WOODEN_PICKAXE); ItemMeta meta = woodenPickage.getItemMeta(); ((Damageable) meta).setDamage(damage); meta.setUnbreakable(true); woodenPickage.setItemMeta(meta); return woodenPickage; }
Example #19
Source File: Util.java From QuickShop-Reremake with GNU General Public License v3.0 | 5 votes |
/** * Gets the percentage (Without trailing %) damage on a tool. * * @param item The ItemStack of tools to check * @return The percentage 'health' the tool has. (Opposite of total damage) */ @NotNull public static String getToolPercentage(@NotNull ItemStack item) { if (!(item.getItemMeta() instanceof Damageable)) { Util.debugLog(item.getType().name() + " not Damageable."); return "Error: NaN"; } double dura = ((Damageable) item.getItemMeta()).getDamage(); double max = item.getType().getMaxDurability(); DecimalFormat formatter = new DecimalFormat("0"); return formatter.format((1 - dura / max) * 100.0); }
Example #20
Source File: ItemStackImpl.java From Civs with GNU General Public License v3.0 | 4 votes |
/** @deprecated */ @Deprecated public short getDurability() { ItemMeta meta = this.getItemMeta(); return meta == null ? 0 : (short)((Damageable)meta).getDamage(); }
Example #21
Source File: ElytraEnhanceListener.java From NyaaUtils with MIT License | 4 votes |
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) public void playerMove(PlayerMoveEvent e) { Player player = e.getPlayer(); if (player.isGliding() && plugin.cfg.elytra_enhance_enabled && !plugin.cfg.disabled_world.contains(player.getWorld().getName()) && player.getLocation().getBlock().isEmpty() && player.getEyeLocation().getBlock().isEmpty() && !disableFuelMode.contains(player.getUniqueId()) && !player.isSneaking()) { if (!FuelMode.contains(player.getUniqueId()) && player.getVelocity().length() >= 0.75 && plugin.fuelManager.getFuelAmount(player, false) > 0) { FuelMode.add(player.getUniqueId()); } if (duration.containsKey(player.getUniqueId()) && duration.get(player.getUniqueId()) >= System.currentTimeMillis()) { player.setVelocity(player.getEyeLocation().getDirection().multiply(plugin.cfg.elytra_max_velocity)); } if (FuelMode.contains(player.getUniqueId()) && player.getVelocity().length() <= plugin.cfg.elytra_min_velocity && player.getLocation().getBlockY() <= plugin.cfg.elytra_boost_max_height && player.getLocation().getPitch() < 50) { if (player.getInventory().getChestplate() != null && player.getInventory().getChestplate().getType() == Material.ELYTRA) { int durability = player.getInventory().getChestplate().getType().getMaxDurability() - ((Damageable)player.getInventory().getChestplate().getItemMeta()).getDamage(); if (durability <= plugin.cfg.elytra_durability_notify) { player.sendMessage(I18n.format("user.elytra_enhance.durability_notify", durability)); } } if (!plugin.fuelManager.useFuel(player)) { FuelMode.remove(player.getUniqueId()); if (duration.containsKey(player.getUniqueId())) { duration.remove(player.getUniqueId()); } return; } else { duration.put(player.getUniqueId(), System.currentTimeMillis() + (plugin.cfg.elytra_power_duration * 1000)); player.setVelocity(player.getEyeLocation().getDirection().multiply(plugin.cfg.elytra_max_velocity)); } int fuelAmount = plugin.fuelManager.getFuelAmount(player, false); if (fuelAmount <= plugin.cfg.elytra_fuel_notify) { player.sendMessage(I18n.format("user.elytra_enhance.fuel_notify", fuelAmount)); } } return; } else if (FuelMode.contains(player.getUniqueId())) { FuelMode.remove(player.getUniqueId()); } }
Example #22
Source File: RepairEffect.java From Civs with GNU General Public License v3.0 | 4 votes |
@EventHandler(ignoreCancelled = true) public void onPlayerInteract(PlayerInteractEvent event) { if (event.getClickedBlock() == null || event.getHand() == null) { return; } if ((!event.getAction().equals(Action.RIGHT_CLICK_BLOCK) && !event.getAction().equals(Action.LEFT_CLICK_BLOCK)) || (!event.getClickedBlock().getType().equals(Material.IRON_BLOCK)) || event.getHand().equals(EquipmentSlot.HAND)) { return; } Region r = RegionManager.getInstance().getRegionAt(event.getClickedBlock().getLocation()); if (r == null || !r.getEffects().containsKey(KEY)) { return; } Player player = event.getPlayer(); ItemStack item = player.getInventory().getItemInMainHand(); ItemMeta itemMeta = item.getItemMeta(); if (!(itemMeta instanceof Damageable)) { return; } Damageable damageable = (Damageable) itemMeta; if (getRequiredReagent(item.getType()).isEmpty()) { return; } if (item.getType() == Material.AIR) { player.sendMessage(Civs.getPrefix() + LocaleManager.getInstance().getTranslationWithPlaceholders(player, "hold-repair-item")); return; } if (!damageable.hasDamage()) { return; } repairItem(event, player, item); }
Example #23
Source File: RepairCommands.java From NyaaUtils with MIT License | 4 votes |
@SubCommand(value = "full", permission = "nu.repair") public void repairFull(CommandSender sender, Arguments args) { ItemStack item = getItemInHand(sender); ItemMeta itemMeta = item.getItemMeta(); ItemStack material = getItemInOffHand(sender); RepairInstance info = new RepairInstance(item, plugin.cfg.repair, plugin); if (info.stat != REPAIRABLE) { msg(sender, "user.repair.info_3", I18n.format("user.repair.unrepairable." + info.stat.name())); return; } if (material.getType() != info.repairMaterial || material.getAmount() <= 0) { msg(sender, "user.repair.material_mismatch"); return; } Player p = asPlayer(sender); if (ExperienceUtils.getExpPoints(p) < info.expConsumption) { msg(sender, "user.repair.no_enough_exp"); return; } int expMax = (int) Math.floor(ExperienceUtils.getExpPoints(p) / (double) info.expConsumption); int materialMax = material.getAmount(); int durMax = (int) Math.ceil(((Damageable) itemMeta).getDamage() / (double) info.durRecovered); int repairAmount = Math.min(Math.min(expMax, materialMax), durMax); if (ExperienceUtils.getExpPoints(p) < info.expConsumption * repairAmount) { msg(sender, "user.repair.no_enough_exp"); return; } ExperienceUtils.subtractExpPoints(p, info.expConsumption * repairAmount); int dur = ((Damageable) itemMeta).getDamage(); dur -= info.durRecovered * repairAmount; if (dur < 0) dur = 0; ((Damageable) itemMeta).setDamage(dur); item.setItemMeta(itemMeta); increaseReapirCount(item, 1); p.getInventory().setItemInMainHand(item); int count = material.getAmount() - repairAmount; if (count <= 0) { p.getInventory().setItemInOffHand(new ItemStack(Material.AIR)); } else { material.setAmount(count); p.getInventory().setItemInOffHand(material); } msg(p, "user.repair.repaired"); }
Example #24
Source File: RepairInstance.java From NyaaUtils with MIT License | 4 votes |
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(); Damageable damageableMeta = (Damageable) item.getItemMeta(); repairLimit = cfg.repairLimit; if (repairLimit > 0 && repairableMeta.getRepairCost() >= repairLimit) { stat = RepairStat.UNREPAIRABLE_RLE; } Material toolMaterial = item.getType(); repairMaterial = cfg.material; int currentDurability = damageableMeta.getDamage(); if (currentDurability <= 0) { stat = RepairStat.UNREPAIRABLE_REPAIRED; } int enchLevel = 0; for (Enchantment ench : Enchantment.values()) { enchLevel += Math.max(item.getEnchantmentLevel(ench), 0); } 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; } }
Example #25
Source File: ItemTools.java From StackMob-3 with GNU General Public License v3.0 | 4 votes |
public static boolean hasEnoughDurability(ItemStack item, int stackSize) { Damageable meta = (Damageable) item.getItemMeta(); int newDamage = meta.getDamage() + stackSize; return newDamage <= item.getType().getMaxDurability(); }
Example #26
Source File: ItemTools.java From StackMob-3 with GNU General Public License v3.0 | 4 votes |
public static ItemStack damageItem(ItemStack item, int stackSize){ Damageable meta = (Damageable) item.getItemMeta(); meta.setDamage(meta.getDamage() + stackSize); item.setItemMeta((ItemMeta) meta); return item; }
Example #27
Source File: ContainerShop.java From QuickShop-Reremake with GNU General Public License v3.0 | 4 votes |
/** * @return The durability of the item */ @Override public short getDurability() { return (short) ((Damageable) this.item.getItemMeta()).getDamage(); }
Example #28
Source File: SelfRepair.java From MineTinker with GNU General Public License v3.0 | 4 votes |
@EventHandler(ignoreCancelled = true) public void effect(PlayerItemDamageEvent event) { if (useMending) { return; } ItemStack tool = event.getItem(); if (!modManager.isToolViable(tool) && !modManager.isArmorViable(tool)) { return; } if (!event.getPlayer().hasPermission("minetinker.modifiers.selfrepair.use")) { return; } if (!modManager.hasMod(tool, this)) { return; } int level = modManager.getModLevel(tool, this); Random rand = new Random(); int n = rand.nextInt(100); int c = this.percentagePerLevel * level; if (n <= c) { if (tool.getItemMeta() instanceof Damageable) { Damageable damageable = (Damageable) tool.getItemMeta(); int dura = damageable.getDamage(); int newDura = dura - this.healthRepair; if (newDura < 0) { newDura = 0; } damageable.setDamage(newDura); tool.setItemMeta((ItemMeta) damageable); ChatWriter.logModifier(event.getPlayer(), event, this, tool, String.format("Chance(%d/%d)", n, c), String.format("Repair(%d -> %d)", dura, newDura)); return; } } ChatWriter.logModifier(event.getPlayer(), event, this, tool, String.format("Chance(%d/%d)", n, c)); }
Example #29
Source File: ItemListener.java From MineTinker with GNU General Public License v3.0 | 4 votes |
@EventHandler(ignoreCancelled = true) public void onItemBreak(PlayerItemBreakEvent event) { Player player = event.getPlayer(); ItemStack item = event.getBrokenItem(); if (!MineTinker.getPlugin().getConfig().getBoolean("UnbreakableTools", true)) { return; } if (Lists.WORLDS.contains(player.getWorld().getName())) { return; } if (!modManager.isToolViable(item) && !modManager.isArmorViable(item)) { return; } if (!MineTinker.getPlugin().getConfig().getBoolean("ItemBehaviour.StopBreakEvent", true)) { return; } if (MineTinker.getPlugin().getConfig().getBoolean("ItemBehaviour.AlertPlayerOnBreak", true)) { player.sendMessage(LanguageManager.getString("Alert.OnItemBreak", player)); } ItemMeta meta = item.getItemMeta(); if (meta instanceof Damageable) { ((Damageable) meta).setDamage(item.getType().getMaxDurability() - 1); } if (meta instanceof CrossbowMeta) { //TODO: Crossbow will still shoot arrow ((CrossbowMeta) meta).setChargedProjectiles(new ArrayList<>()); } item.setItemMeta(meta); if (player.getInventory().addItem(item).size() != 0) { //adds items to (full) inventory if (!MineTinker.getPlugin().getConfig().getBoolean("ItemBehaviour.DisableDroppingBehaviour")) { PlayerDropItemEvent dropItemEvent = new PlayerDropItemEvent(player, player.getWorld().dropItem(player.getLocation(), item)); Bukkit.getPluginManager().callEvent(dropItemEvent); //To trigger item behaviour } else { player.getWorld().dropItem(player.getLocation(), item); } } // no else as it gets added in if-clause }