Skip to content

Commit

Permalink
Avoid exception and crash scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
mbax committed Aug 25, 2016
1 parent 61f2a16 commit b1a000d
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/main/java/org/kitteh/tim/Tim.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ private int getEnchantmentLevel(CommandContext commandContext) throws CommandExc
level = Integer.parseInt(levelString);
if (level < 1) {
throw new CommandException(this.getErrorText("Enchantment level has to be greater than 0"));
} else if (level > Short.MAX_VALUE) {
throw new CommandException(this.getErrorText("Enchantment level can't be higher than " + Short.MAX_VALUE));
}
return level;
} catch (NumberFormatException e) {
Expand All @@ -164,7 +166,9 @@ private void enchant(Player player, Enchantment enchantment, int level) throws C
ItemStack item = player.getItemInHand(HandTypes.MAIN_HAND).orElseThrow(() -> new CommandException(this.getErrorText("You need to be holding an item to enchant it!")));
item.transform(Keys.ITEM_ENCHANTMENTS, list -> {
List<ItemEnchantment> newList = new LinkedList<>();
list.stream().filter(ench -> ench.getEnchantment() != enchantment).forEach(newList::add);
if (list != null) {
list.stream().filter(ench -> ench.getEnchantment() != enchantment).forEach(newList::add);
}
newList.add(new ItemEnchantment(enchantment, level));
return newList;
});
Expand Down

0 comments on commit b1a000d

Please sign in to comment.