public boolean addCustomEffect(PotionEffect effect, boolean overwrite) { Validate.notNull(effect, "Potion effect must not be null"); int index = indexOfEffect(effect.getType()); if (index != -1) { if (overwrite) { PotionEffect old = customEffects.get(index); if (old.getAmplifier() == effect.getAmplifier() && old.getDuration() == effect.getDuration() && old.isAmbient() == effect.isAmbient()) { return false; } customEffects.set(index, effect); return true; } else { return false; } } else { if (customEffects == null) { customEffects = new ArrayList<PotionEffect>(); } customEffects.add(effect); return true; } }
public static void savePotion(ItemStack potion, String name) { if (potion.getType() != Material.POTION) return; PotionMeta meta = (PotionMeta) potion.getItemMeta(); ConfigurationSection potionYaml = yaml.createSection(name); for (int i = 0; i < meta.getCustomEffects().size(); i++) { PotionEffect effect = meta.getCustomEffects().get(i); ConfigurationSection effectYaml = potionYaml.createSection("effect_" + i); effectYaml.set("type", effect.getType().getName().toLowerCase()); effectYaml.set("duration", effect.getDuration()); effectYaml.set("amplifier", effect.getAmplifier() + 1); effectYaml.set("showParticles", effect.isAmbient()); } }
@Override void applyToItem(NBTTagCompound tag) { super.applyToItem(tag); if (hasCustomEffects()) { NBTTagList effectList = new NBTTagList(); tag.set(POTION_EFFECTS.NBT, effectList); for (PotionEffect effect : customEffects) { NBTTagCompound effectData = new NBTTagCompound(); effectData.setByte(ID.NBT, (byte) effect.getType().getId()); effectData.setByte(AMPLIFIER.NBT, (byte) effect.getAmplifier()); effectData.setInt(DURATION.NBT, effect.getDuration()); effectData.setBoolean(AMBIENT.NBT, effect.isAmbient()); effectList.add(effectData); } } }
@Override public void tick(Player player) { if (item.getHasPermission() == true && player.hasPermission(item.getPermission()) == false) { player.sendMessage(ChatColor.RED + String.format(Locale.get("message.error.permission"))); } else { boolean hasEffect = false; for (PotionEffect potionEffect : player.getActivePotionEffects()) { if (potionEffect.getType().equals(effect)) { hasEffect = true; if (potionEffect.getDuration() <= 10) player.addPotionEffect(new PotionEffect(effect, 30, amplifier, true), true); break; } } if (!hasEffect) { player.addPotionEffect(new PotionEffect(effect, 30, amplifier, true), true); } } }
/** * Scores the specified {@link PotionEffect}. * * @param effect The effect to score. * @return The score. * @throws IllegalArgumentException If an effect with an unknown {@link PotionEffectType} was * specified. */ public static double getScore(final PotionEffect effect) throws IllegalArgumentException { Preconditions.checkNotNull(effect, "Effect"); // Duration, in ticks int duration = effect.getDuration(); // Level (>= 1 is normal effect, == 0 is no effect, < 0 is inverse effect) int level = effect.getAmplifier(); PotionEffectType effectType = effect.getType(); PotionClassification classification = (level >= 0 ? PotionUtils.potionEffectTypeImplications.get(effectType) : PotionUtils.inversePotionEffectTypeImplications.get(effectType)); if (effectType == null || classification == null) throw new IllegalArgumentException("Unknown (or null) PotionEffectType"); double score = classification.getScore(); score *= Math.abs(level); return score * ((double) duration / 20); }
@Override public void apply(LivingEntity target) { if (potionEffectType == null) { return; } int dur = duration / MS_PER_TICK; if (target.hasPotionEffect(potionEffectType)) { PotionEffect potionEffect = null; for (PotionEffect potEff : target.getActivePotionEffects()) { if (potEff.getType() == potionEffectType) { potionEffect = potEff; break; } } if (potionEffect != null) { dur += potionEffect.getDuration(); } } target.addPotionEffect(new PotionEffect(potionEffectType, dur, intensity)); }
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onPotionSplash(final PotionSplashEvent event) { ThrownPotion potion = event.getPotion(); Collection<PotionEffect> effects = ((PotionMeta) potion.getItem().getItemMeta()).getCustomEffects(); Bukkit.broadcastMessage( "A " + event.getEntity().getType().getName() + " splashed a potion with " + effects.size() + " effects attached:"); for (PotionEffect effect : effects) { Bukkit.broadcastMessage( "Type: " + effect.getType().getName() + ", Level: " + (effect.getAmplifier() + 1) + ", Duration: " + effect.getDuration()); } }
@EventHandler public static void addPotionInfo(ItemInfoEvent event) { ItemStack item = event.getItem(); if (item.getType() != Material.POTION || item.getDurability() == 0) { return; } Potion potion = Potion.fromItemStack(item); StringBuilder message = new StringBuilder(50); message.append(ChatColor.GRAY); if (potion.getType() == null) { return; } if (potion.isSplash()) { message.append("Splash "); } message.append("Potion of "); message.append(capitalizeFirstLetter(potion.getType().name(), '_')).append(' '); message.append(toRoman(potion.getLevel())); CommandSender sender = event.getSender(); sender.sendMessage(message.toString()); for (PotionEffect effect : potion.getEffects()) { sender.sendMessage( ChatColor.DARK_GRAY + capitalizeFirstLetter(effect.getType().getName(), '_') + ' ' + toTime(effect.getDuration() / 20)); } }
public static String getPotionToString(PotionEffect pe) { return (pe.getType().getId() + ":" + pe.getDuration() + ":" + pe.getAmplifier()); }
/** * Sends a display of item information to the specified command sender * * @param item the item to display, cannot be null * @param sender the command sender to send the display to, cannot be null */ public static void showInformation(ItemStack item, CommandSender sender) { if (item == null || sender == null) throw new IllegalArgumentException(); StringBuilder builder = new StringBuilder(); builder.append(ChatColor.BOLD).append(getName(item)).append(" "); if (item.hasItemMeta() && item.getItemMeta().hasDisplayName()) { builder.append(ChatColor.ITALIC).append("(").append(getName(item, true)).append(") "); } builder.append(ChatColor.BLUE).append("x").append(item.getAmount()); DumbAuction.getInstance().sendMessage(sender, ChatColor.AQUA + builder.toString().trim()); // Display durability if (item.getType().getMaxDurability() > 0 && item.getDurability() != 0) { double durability = 1 - ((double) item.getDurability() / (double) item.getType().getMaxDurability()); int percent = (int) Math.round(durability * 100); DumbAuction.getInstance() .sendMessage( sender, ChatColor.GRAY + "Durability: " + ChatColor.AQUA + "" + percent + "%"); } Potion pot = null; try { pot = Potion.fromItemStack(item); } catch (Exception e) { } // Consume error List<String> metaMessage = new ArrayList<String>(); if (item.hasItemMeta() || pot != null) { ItemMeta meta = item.getItemMeta(); if (meta.hasLore()) { List<String> lore = meta.getLore(); metaMessage.add(ChatColor.LIGHT_PURPLE + "Lore: "); metaMessage.add(ChatColor.DARK_GRAY + "-------------"); for (String l : lore) { metaMessage.add(ChatColor.GRAY + l); } metaMessage.add(ChatColor.DARK_GRAY + "-------------"); } if (meta.hasEnchants() || meta instanceof EnchantmentStorageMeta) { metaMessage.add(ChatColor.LIGHT_PURPLE + "Enchants: "); metaMessage.add(ChatColor.DARK_GRAY + "-------------"); if (meta.hasEnchants()) { Map<Enchantment, Integer> enchants = meta.getEnchants(); for (Enchantment e : enchants.keySet()) { int level = enchants.get(e); String strLevel = integerToRomanNumeral(level) + " " + ChatColor.GRAY + "(" + level + ")"; metaMessage.add(ChatColor.AQUA + getEnchantmentName(e) + " " + strLevel); } } if (meta instanceof EnchantmentStorageMeta) { EnchantmentStorageMeta emeta = (EnchantmentStorageMeta) meta; if (emeta.hasStoredEnchants()) { Map<Enchantment, Integer> enchants = emeta.getStoredEnchants(); for (Enchantment e : enchants.keySet()) { int level = enchants.get(e); String strLevel = integerToRomanNumeral(level) + " " + ChatColor.GRAY + "(" + level + ")"; metaMessage.add(ChatColor.AQUA + getEnchantmentName(e) + " " + strLevel); } } } metaMessage.add(ChatColor.DARK_GRAY + "-------------"); } if (meta instanceof BookMeta) { BookMeta book = (BookMeta) meta; if (book.hasTitle()) metaMessage.add(ChatColor.GRAY + "Book Title: " + ChatColor.AQUA + book.getTitle()); if (book.hasAuthor()) metaMessage.add(ChatColor.GRAY + "Book Author: " + ChatColor.AQUA + book.getAuthor()); } List<FireworkEffect> effects = new ArrayList<FireworkEffect>(); int fireworkPower = -1; if (meta instanceof FireworkEffectMeta) { FireworkEffectMeta firework = (FireworkEffectMeta) meta; if (firework.hasEffect()) { effects.add(firework.getEffect()); } } if (meta instanceof FireworkMeta) { FireworkMeta firework = (FireworkMeta) meta; if (firework.hasEffects()) { effects.addAll(firework.getEffects()); } fireworkPower = firework.getPower(); } if (effects.size() > 0) { metaMessage.add(ChatColor.LIGHT_PURPLE + "Firework Effects: "); metaMessage.add(ChatColor.DARK_GRAY + "-------------"); for (FireworkEffect effect : effects) { metaMessage.add(ChatColor.AQUA + getFireworkTypeName(effect.getType())); if (effect.getColors().size() > 0) { builder = new StringBuilder(); for (Color color : effect.getColors()) { ChatColor chat = ChatColorPalette.matchColor(color.getRed(), color.getGreen(), color.getBlue()); String name = getChatName(chat); builder.append(chat).append(name).append(ChatColor.GRAY).append(", "); } metaMessage.add( ChatColor.GRAY + " Colors: " + builder.toString().substring(0, builder.toString().length() - 2)); } if (effect.getFadeColors().size() > 0) { builder = new StringBuilder(); for (Color color : effect.getFadeColors()) { ChatColor chat = ChatColorPalette.matchColor(color.getRed(), color.getGreen(), color.getBlue()); String name = getChatName(chat); builder.append(chat).append(name).append(ChatColor.GRAY).append(", "); } metaMessage.add( ChatColor.GRAY + " Fade Colors: " + builder.toString().substring(0, builder.toString().length() - 2)); } } metaMessage.add(ChatColor.DARK_GRAY + "-------------"); } if (fireworkPower >= 0) { metaMessage.add(ChatColor.GRAY + "Firework Power: " + ChatColor.AQUA + "" + fireworkPower); } if (meta instanceof LeatherArmorMeta) { LeatherArmorMeta leather = (LeatherArmorMeta) meta; if (!leather .getColor() .equals( DumbAuction.getInstance().getServer().getItemFactory().getDefaultLeatherColor())) { ChatColor chat = ChatColorPalette.matchColor( leather.getColor().getRed(), leather.getColor().getGreen(), leather.getColor().getBlue()); metaMessage.add(ChatColor.GRAY + "Leather Color: " + chat + getChatName(chat)); } } if (meta instanceof SkullMeta) { SkullMeta skull = (SkullMeta) meta; if (skull.hasOwner()) { metaMessage.add(ChatColor.GRAY + "Skull Player: " + ChatColor.AQUA + skull.getOwner()); } } if (meta instanceof PotionMeta || pot != null) { metaMessage.add(ChatColor.LIGHT_PURPLE + "Potion Effects: "); metaMessage.add(ChatColor.DARK_GRAY + "-------------"); if (pot != null) { for (PotionEffect effect : pot.getEffects()) { int amplifier = effect.getAmplifier() + 1; int time = effect.getDuration() / 20; metaMessage.add( ChatColor.AQUA + getPotionEffectName(effect.getType()) + " " + integerToRomanNumeral(amplifier) + " " + ChatColor.GRAY + "(" + amplifier + ") for " + ChatColor.AQUA + toTime(time)); } } if (meta instanceof PotionMeta) { PotionMeta potion = (PotionMeta) meta; if (potion.hasCustomEffects()) { for (PotionEffect effect : potion.getCustomEffects()) { int amplifier = effect.getAmplifier() + 1; int time = effect.getDuration() / 20; metaMessage.add( ChatColor.AQUA + getPotionEffectName(effect.getType()) + " " + integerToRomanNumeral(amplifier) + " " + ChatColor.GRAY + "(" + amplifier + ") for " + ChatColor.AQUA + toTime(time)); } } } metaMessage.add(ChatColor.DARK_GRAY + "-------------"); } // Note: MapMeta is useless and not used } for (String s : metaMessage) { DumbAuction.getInstance().sendMessage(sender, s); } }