/**
   * Adds the specified {@link Enchantment} to this item stack.
   *
   * <p>If this item stack already contained the given enchantment (at any level), it will be
   * replaced.
   *
   * @param ench Enchantment to add
   * @param level Level of the enchantment
   * @throws IllegalArgumentException if enchantment null, or enchantment is not applicable
   */
  @Utility
  public void addEnchantment(Enchantment ench, int level) {
    Validate.notNull(ench, "Enchantment cannot be null");
    if ((level < ench.getStartLevel()) || (level > ench.getMaxLevel())) {
      throw new IllegalArgumentException(
          "Enchantment level is either too low or too high (given "
              + level
              + ", bounds are "
              + ench.getStartLevel()
              + " to "
              + ench.getMaxLevel()
              + ")");
    } else if (!ench.canEnchantItem(this)) {
      throw new IllegalArgumentException(
          "Specified enchantment cannot be applied to this itemstack");
    }

    addUnsafeEnchantment(ench, level);
  }
Exemple #2
0
  /**
   * Process results from Fishing.
   *
   * @param event The event to modify
   */
  public static void processResults(PlayerFishEvent event) {
    Player player = event.getPlayer();
    PlayerProfile PP = Users.getProfile(player);

    getFishingResults(player, event);
    Item theCatch = (Item) event.getCaught();

    if (theCatch.getItemStack().getType() != Material.RAW_FISH) {
      final int ENCHANTMENT_CHANCE = 10;
      boolean enchanted = false;
      ItemStack fishingResults = theCatch.getItemStack();

      player.sendMessage(mcLocale.getString("Fishing.ItemFound"));
      if (ItemChecks.isArmor(fishingResults) || ItemChecks.isTool(fishingResults)) {
        if (random.nextInt(100) <= ENCHANTMENT_CHANCE) {
          for (Enchantment newEnchant : Enchantment.values()) {
            if (newEnchant.canEnchantItem(fishingResults)) {
              Map<Enchantment, Integer> resultEnchantments = fishingResults.getEnchantments();

              for (Enchantment oldEnchant : resultEnchantments.keySet()) {
                if (oldEnchant.conflictsWith(newEnchant)) {
                  return;
                }
              }

              /* Actual chance to have an enchantment is related to your fishing skill */
              if (random.nextInt(15) < Fishing.getFishingLootTier(PP)) {
                enchanted = true;
                int randomEnchantLevel = random.nextInt(newEnchant.getMaxLevel()) + 1;

                if (randomEnchantLevel < newEnchant.getStartLevel()) {
                  randomEnchantLevel = newEnchant.getStartLevel();
                }

                fishingResults.addEnchantment(newEnchant, randomEnchantLevel);
              }
            }
          }
        }
      }

      if (enchanted) {
        player.sendMessage(mcLocale.getString("Fishing.MagicFound"));
      }
    }
  }
Exemple #3
0
  public static void enchantInventory(Player player) {
    ItemStack[] itemStack = player.getInventory().getContents();
    Enchantment[] enchantments = Enchantment.values();

    for (ItemStack item : itemStack) {
      if (item != null) {
        for (Enchantment enchantment : enchantments) {
          if (enchantment != null) {
            try {
              item.addEnchantment(enchantment, enchantment.getMaxLevel());
            } catch (Exception ex) {
            } // Ignore
          }
        }
      }
    }
  }
 public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
   if (sender.hasPermission("bukkit.command.enchant")) {
     Player p = Bukkit.getPlayerExact(sender.getName());
     if (args.length == 0) {
       List<String> names = new ArrayList<String>();
       for (Enchantment enchant : Enchantment.values()) names.add(enchant.getName().toUpperCase());
       sender.sendMessage(
           ChatColor.GREEN
               + "Enchants: |"
               + ChatColor.RED
               + StringUtils.join(names, ChatColor.GREEN + "|  |" + ChatColor.RED)
               + ChatColor.GREEN
               + "|");
     } else {
       Enchantment enchant = Enchantment.getByName(args[0].toUpperCase());
       if (enchant == null) {
         for (Enchantment enchantment : Enchantment.values())
           if (enchantment.getName().equalsIgnoreCase(args[0])) enchant = enchantment;
         if (enchant == null) {
           sender.sendMessage(ChatColor.GREEN + "Enchant: " + args[0] + " - Not found");
           return true;
         }
       }
       int level = enchant.getMaxLevel();
       if (args.length > 1) {
         if (lib.isNumeric(args[1])) level = Integer.parseInt(args[1]);
       }
       if (p.getItemInHand() != null && p.getItemInHand().getType() != Material.AIR) {
         p.getItemInHand().addUnsafeEnchantment(enchant, level);
         p.sendMessage(
             ChatColor.GREEN
                 + "Item enchanted with "
                 + enchant.getName()
                 + " with level "
                 + level);
         updateEnchants(p.getItemInHand());
       } else p.sendMessage(ChatColor.GREEN + "Unable to enchant item");
     }
   } else sender.sendMessage(ChatColor.RED + "You do not have permission to use this command");
   return true;
 }
	//TODO: Implement charge costs: final Trade charge = new Trade("enchant-" + enchantmentName, ess);
	@Override
	protected void run(final IUser user, final String commandLabel, final String[] args) throws Exception
	{
		final ItemStack stack = user.getPlayer().getItemInHand();
		if (stack == null)
		{
			throw new Exception(_("nothingInHand"));
		}
		if (args.length == 0)
		{
			final Set<String> enchantmentslist = new TreeSet<String>();
			for (Map.Entry<String, Enchantment> entry : Enchantments.entrySet())
			{
				final String enchantmentName = entry.getValue().getName().toLowerCase(Locale.ENGLISH);
				if (enchantmentslist.contains(enchantmentName) || Permissions.ENCHANT.isAuthorized(user, enchantmentName))
				{
					enchantmentslist.add(entry.getKey());
					//enchantmentslist.add(enchantmentName);
				}
			}
			throw new NotEnoughArgumentsException(_("enchantments", Util.joinList(enchantmentslist.toArray())));
		}
		int level = -1;
		if (args.length > 1)
		{
			try
			{
				level = Integer.parseInt(args[1]);
			}
			catch (NumberFormatException ex)
			{
				level = -1;
			}
		}
		final boolean allowUnsafe = Permissions.ENCHANT_UNSAFE.isAuthorized(user);
		final Enchantment enchantment = getEnchantment(args[0], user);
		if (level < 0 || (!allowUnsafe && level > enchantment.getMaxLevel()))
		{
			level = enchantment.getMaxLevel();
		}
		if (level == 0)
		{
			stack.removeEnchantment(enchantment);
		}
		else
		{
			if (allowUnsafe)
			{
				stack.addUnsafeEnchantment(enchantment, level);
			}
			else
			{
				stack.addEnchantment(enchantment, level);
			}
		}
		user.getPlayer().getInventory().setItemInHand(stack);
		user.getPlayer().updateInventory();
		final String enchantmentName = enchantment.getName().toLowerCase(Locale.ENGLISH);
		if (level == 0)
		{
			user.sendMessage(_("enchantmentRemoved", enchantmentName.replace('_', ' ')));
		}
		else
		{
			user.sendMessage(_("enchantmentApplied", enchantmentName.replace('_', ' ')));
		}
	}
Exemple #6
0
	public static void expandItems(final IEssentials ess, final User user, final List<String> items) throws Exception
	{
		try
		{
			boolean spew = false;
			for (String d : items)
			{
				final String[] parts = d.split(" ");
				final String[] item = parts[0].split("[:+',;.]", 2);
				final int id = Material.getMaterial(Integer.parseInt(item[0])).getId();
				final short data = item.length > 1 ? Short.parseShort(item[1]) : 0;
				final int amount = parts.length > 1 ? Integer.parseInt(parts[1]) : 1;

				final ItemStack stack = new ItemStack(id, amount, data);
				if (parts.length > 2)
				{
					for (int i = 2; i < parts.length; i++)
					{
						final String[] split = parts[i].split("[:+',;.]", 2);
						if (split.length < 1)
						{
							continue;
						}
						final Enchantment enchantment = Commandenchant.getEnchantment(split[0], user);
						int level;
						if (split.length > 1)
						{
							level = Integer.parseInt(split[1]);
						}
						else
						{
							level = enchantment.getMaxLevel();
						}
						stack.addEnchantment(enchantment, level);
					}
				}

				final Map<Integer, ItemStack> overfilled;
				if (user.isAuthorized("essentials.oversizedstacks"))
				{
					overfilled = InventoryWorkaround.addItem(user.getInventory(), true, ess.getSettings().getOversizedStackSize(), stack);
				}
				else
				{
					overfilled = InventoryWorkaround.addItem(user.getInventory(), true, new ItemStack(id, amount, data));
				}
				for (ItemStack itemStack : overfilled.values())
				{
					user.getWorld().dropItemNaturally(user.getLocation(), itemStack);
					spew = true;
				}
			}
			user.updateInventory();
			if (spew)
			{
				user.sendMessage(_("kitInvFull"));
			}
		}
		catch (Exception e)
		{
			user.updateInventory();
			throw new Exception(_("kitError2"));
		}
	}
	@Override
	public void run(final CommandSender sender, final String commandLabel, final String[] args) throws Exception
	{
		if (args.length < 2)
		{
			throw new NotEnoughArgumentsException();
		}
		
		final IUser giveTo = getPlayer(args, 0);

		final ItemStack stack = ess.getItemDb().get(args[1], giveTo);

		final String itemname = stack.getType().toString().toLowerCase(Locale.ENGLISH).replace("_", "");
		if (!GivePermissions.getPermission(stack.getType()).isAuthorized(sender))
		{
			throw new Exception(_("cantSpawnItem", itemname));
		}

		if (args.length > 3 && Util.isInt(args[2]) && Util.isInt(args[3]))
		{			
			stack.setAmount(Integer.parseInt(args[2]));
			stack.setDurability(Short.parseShort(args[3]));
		}
		else if (args.length > 2 && Integer.parseInt(args[2]) > 0)
		{
			stack.setAmount(Integer.parseInt(args[2]));
		}		

		if (args.length > 3)
		{
			for (int i = Util.isInt(args[3]) ? 4 : 3; i < args.length; i++)
			{
				final String[] split = args[i].split("[:+',;.]", 2);
				if (split.length < 1)
				{
					continue;
				}
				final Enchantment enchantment = Commandenchant.getEnchantment(split[0], sender instanceof Player ? ess.getUser((Player)sender) : null);
				int level;
				if (split.length > 1)
				{
					level = Integer.parseInt(split[1]);
				}
				else
				{
					level = enchantment.getMaxLevel();
				}
				stack.addEnchantment(enchantment, level);
			}
		}

		if (stack.getType() == Material.AIR)
		{
			throw new Exception(_("cantSpawnItem", "Air"));
		}
		
		giveTo.giveItems(stack, false);

		//TODO: TL this.
		final String itemName = stack.getType().toString().toLowerCase(Locale.ENGLISH).replace('_', ' ');
		sender.sendMessage(ChatColor.BLUE + "Giving " + stack.getAmount() + " of " + itemName + " to " + giveTo.getDisplayName() + ".");
		
	}
  @EventHandler
  public void onEntityDeath(final EntityDeathEvent event) {
    int modifier = 1;
    event.setDroppedExp(0);
    String world = event.getEntity().getLocation().getWorld().getName();
    if (world.equalsIgnoreCase("oilrig")) modifier = 3;
    if (world.equalsIgnoreCase("city")) modifier = 2;
    if (!(event.getEntity() instanceof Player)) {
      List<ItemStack> list = event.getDrops();
      if (event.getEntity() instanceof Zombie || event.getEntity() instanceof PigZombie) {
        list.clear();
        if (plugin.gen.nextInt(50) == 0) {
          Material random =
              drop.selector(
                  plugin.gen.nextInt(2) + 1,
                  plugin.gen.nextInt(5) + 1,
                  plugin.gen.nextInt(4) + 1,
                  plugin.gen.nextInt(5) + 1);
          ItemStack item = new ItemStack(random, 1);
          Integer enchamt = plugin.gen.nextInt(10);
          for (int i = 0; i < enchamt; i++) {
            int rand = plugin.gen.nextInt(21) + 1;
            Enchantment enchantment = drop.enchant(rand);
            int power = plugin.gen.nextInt(enchantment.getMaxLevel()) + 1;
            item.addUnsafeEnchantment(enchantment, power);
          }
          list.add(item);
        }
        if (plugin.gen.nextInt(25) == 0) {
          int[] iteml = {296, 297, 263, 264, 265, 266, 260, 289, 352, 287, 368};
          Integer random = plugin.gen.nextInt(iteml.length);
          ItemStack item = new ItemStack(iteml[random], plugin.gen.nextInt(5) + 1);
          list.add(item);
        }
        if (plugin.gen.nextInt(30) == 0) {
          int[] iteml = {372, 376, 375, 377, 370, 378, 369, 262, 341, 367};
          Integer random = plugin.gen.nextInt(iteml.length);
          ItemStack item = new ItemStack(iteml[random], 1);
          list.add(item);
        }
        if (event.getEntity() instanceof Zombie) {
          ArrayList<Player> l = new ArrayList<Player>();
          for (Entity entity : event.getEntity().getNearbyEntities(10, 10, 10)) {
            if (entity instanceof Player) {
              Player killer = (Player) entity;
              killer.setLevel(killer.getLevel() + (1 * modifier));
              l.add(killer);
            }
          }
          EntityDamageEvent e = event.getEntity().getLastDamageCause();
          if (e instanceof EntityDamageByEntityEvent) {
            EntityDamageByEntityEvent ev = (EntityDamageByEntityEvent) e;
            if (ev.getDamager() instanceof Arrow) {
              Arrow a = (Arrow) ev.getDamager();
              if (a.getShooter() instanceof Player) {
                Player killer = (Player) a.getShooter();
                if (!l.contains(killer)) {
                  killer.setLevel(killer.getLevel() + (1 * modifier));
                }
              }
            }
          }
          l.clear();
        }

        if (event.getEntity() instanceof PigZombie) {

          ArrayList<Player> l = new ArrayList<Player>();
          for (Entity entity : event.getEntity().getNearbyEntities(10, 10, 10)) {
            if (entity instanceof Player) {
              Player killer = (Player) entity;
              killer.setLevel(killer.getLevel() + (5 * modifier));
              l.add(killer);
            }
          }
          EntityDamageEvent e = event.getEntity().getLastDamageCause();
          if (e instanceof EntityDamageByEntityEvent) {
            EntityDamageByEntityEvent ev = (EntityDamageByEntityEvent) e;
            if (ev.getDamager() instanceof Arrow) {
              Arrow a = (Arrow) ev.getDamager();
              if (a.getShooter() instanceof Player) {
                Player killer = (Player) a.getShooter();
                if (!l.contains(killer)) {
                  killer.setLevel(killer.getLevel() + (5 * modifier));
                }
              }
            }
          }
          l.clear();
        }
      }

      if (event.getEntity() instanceof Giant) {
        ItemStack item = new ItemStack(57, 10);
        list.add(item);
        StringBuilder sb = new StringBuilder();
        for (Entity entity : event.getEntity().getNearbyEntities(15, 15, 15)) {
          if (entity instanceof Player) {
            Player killer = (Player) entity;
            String name = killer.getName();
            if (name.equalsIgnoreCase("deathmarin")) {
              name = "Deathmarine";
            }
            sb.append(name + ", ");
            killer.setLevel(killer.getLevel() + (50 * modifier));
          }
        }

        plugin.getServer().broadcastMessage(ChatColor.AQUA + sb.toString() + "killed the giant.");

        for (int i = 0; i < 50; i++) {
          event
              .getEntity()
              .getLocation()
              .getWorld()
              .spawn(event.getEntity().getLocation(), Zombie.class);
        }
      }
      if (plugin.gen.nextInt(16) == 0) {
        ItemStack item = new ItemStack(90, 1);
        list.add(item);
      }
    }
  }