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());
   }
 }
 public static ItemStack loadPotion(ItemStack potion, String name) {
   if (potion.getType() != Material.POTION) return potion;
   PotionMeta meta = (PotionMeta) potion.getItemMeta();
   meta.clearCustomEffects();
   ConfigurationSection potionYaml = getSection(name);
   if (potionYaml == null) return potion;
   for (String index : potionYaml.getKeys(false)) {
     ConfigurationSection effectYaml = potionYaml.getConfigurationSection(index);
     PotionEffect effect =
         new PotionEffect(
             ItemController.matchPotionEffect(effectYaml.getString("type")),
             effectYaml.getInt("duration", 600),
             effectYaml.getInt("amplifier", 1) - 1,
             effectYaml.getBoolean("showParticles", true));
     meta.addCustomEffect(effect, true);
   }
   if (!meta.getCustomEffects().isEmpty())
     meta.setMainEffect(meta.getCustomEffects().get(0).getType());
   potion.setItemMeta(meta);
   return potion;
 }
Example #3
0
  public static void transferPotionMeta(PotionMeta meta, JsonObject json, boolean meta2json) {
    if (meta2json) {
      if (!meta.hasCustomEffects()) return;
      json.add(POTION_EFFECTS, convertPotionEffectList(meta.getCustomEffects()));
    } else {
      JsonElement element = json.get(POTION_EFFECTS);
      if (element == null) element = json.get(POTION_EFFECTS_OLD);
      if (element == null) return;

      meta.clearCustomEffects();
      for (PotionEffect pe : convertPotionEffectList(element)) {
        meta.addCustomEffect(pe, false);
      }
    }
  }
  /**
   * 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);
    }
  }