Skip to content

Commit b1a000d

Browse files
committed
Avoid exception and crash scenarios
1 parent 61f2a16 commit b1a000d

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/main/java/org/kitteh/tim/Tim.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ private int getEnchantmentLevel(CommandContext commandContext) throws CommandExc
141141
level = Integer.parseInt(levelString);
142142
if (level < 1) {
143143
throw new CommandException(this.getErrorText("Enchantment level has to be greater than 0"));
144+
} else if (level > Short.MAX_VALUE) {
145+
throw new CommandException(this.getErrorText("Enchantment level can't be higher than " + Short.MAX_VALUE));
144146
}
145147
return level;
146148
} catch (NumberFormatException e) {
@@ -164,7 +166,9 @@ private void enchant(Player player, Enchantment enchantment, int level) throws C
164166
ItemStack item = player.getItemInHand(HandTypes.MAIN_HAND).orElseThrow(() -> new CommandException(this.getErrorText("You need to be holding an item to enchant it!")));
165167
item.transform(Keys.ITEM_ENCHANTMENTS, list -> {
166168
List<ItemEnchantment> newList = new LinkedList<>();
167-
list.stream().filter(ench -> ench.getEnchantment() != enchantment).forEach(newList::add);
169+
if (list != null) {
170+
list.stream().filter(ench -> ench.getEnchantment() != enchantment).forEach(newList::add);
171+
}
168172
newList.add(new ItemEnchantment(enchantment, level));
169173
return newList;
170174
});

0 commit comments

Comments
 (0)