@EventHandler(priority = EventPriority.NORMAL) public void onPlayerDamage(EntityDamageByEntityEvent evt) { if (evt.getDamager() instanceof Player) { Player p = (Player) evt.getDamager(); if (p.hasPotionEffect(PotionEffectType.INCREASE_DAMAGE)) { for (PotionEffect eff : p.getActivePotionEffects()) { if (eff.getType().equals(PotionEffectType.INCREASE_DAMAGE)) { double div = (eff.getAmplifier() + 1) * 1.3D + 1.0D; int dmg; if (evt.getDamage() / div <= 1.0D) { dmg = (eff.getAmplifier() + 1) * 3 + 1; } else { double flatdmg = 2.0; dmg = (int) (evt.getDamage() / div) + (int) ((eff.getAmplifier() + 1) * flatdmg); } evt.setDamage(dmg); break; } } } } }
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()); } }
@EventHandler public void splash(PotionSplashEvent event) { Collection<PotionEffect> effects = event.getPotion().getEffects(); for (PotionEffect effect : effects) { if (effect.getType() == PotionEffectType.INCREASE_DAMAGE) { if (effect.getAmplifier() > 0) { event.setCancelled(true); } } if (effect.getType() == PotionEffectType.INVISIBILITY) { event.setCancelled(true); } } }
@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); } } }
/** * 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); }
@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()); } }
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); } }
@Override public final void check(IProfile player) { final Player p = player.getBukkitPlayer(); if (p.isInsideVehicle()) return; // Riding a horse or minecart makes it possible to move much faster if (p.getAllowFlight()) return; // If they have both speed AND slowness, we're just going to ignore them... if (p.hasPotionEffect(PotionEffectType.SPEED) && !p.hasPotionEffect(PotionEffectType.SLOW)) { PotionEffect potionEffect = null; for (PotionEffect effect : p.getActivePotionEffects()) { if (effect.getType().equals(PotionEffectType.SPEED)) { potionEffect = effect; break; } } double maxBps = this.maxBlocksPerSecond; if (potionEffect != null) { // They have speed int amp = potionEffect.getAmplifier(); maxBps = maxBlocksPerSecond + (maxBlocksPerSecond * ((amp + 1) * 0.20)); // Speed increases by 20% } double bps = player.getProfileData().getBlocksPerSecond(); if (bps > maxBps) { fail(player); } } else if (p.hasPotionEffect(PotionEffectType.SLOW) && !p.hasPotionEffect(PotionEffectType.SPEED)) { // Slowness decreases by 15% * potion level PotionEffect potionEffect = null; for (PotionEffect effect : p.getActivePotionEffects()) { if (effect.getType().equals(PotionEffectType.SLOW)) { potionEffect = effect; break; } } double maxBps = this.maxBlocksPerSecond; if (potionEffect != null) { // They have slow int amp = potionEffect.getAmplifier(); maxBps = maxBlocksPerSecond * (amp - (amp * 0.15)); // Slow decreases by 15% } double bps = player.getProfileData().getBlocksPerSecond(); if (bps > maxBps) { fail(player); } } else if (!p.hasPotionEffect(PotionEffectType.SPEED) && !p.hasPotionEffect(PotionEffectType.SLOW)) { double bps = player.getProfileData().getBlocksPerSecond(); if (bps > this.maxBlocksPerSecond) { fail(player); } } // Reset their blocks per second player.getProfileData().setBlocksPerSecond(0); }