diff options
author | Roman / Linnea Gräf <roman.graef@gmail.com> | 2022-09-09 15:52:20 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-09 15:52:20 +0200 |
commit | d203640d5296bde151596388aa6df332393d0a1c (patch) | |
tree | efecc826c844620e586351454ea6cfcf811ce9e5 | |
parent | 8b53b538debb6285ab85877939b66f872c991f32 (diff) | |
download | NotEnoughUpdates-d203640d5296bde151596388aa6df332393d0a1c.tar.gz NotEnoughUpdates-d203640d5296bde151596388aa6df332393d0a1c.tar.bz2 NotEnoughUpdates-d203640d5296bde151596388aa6df332393d0a1c.zip |
Fix item resolution query crashing on some enchant books (#262)
-rw-r--r-- | src/main/java/io/github/moulberry/notenoughupdates/util/ItemResolutionQuery.java | 4 | ||||
-rw-r--r-- | src/main/java/io/github/moulberry/notenoughupdates/util/IteratorUtils.java | 36 |
2 files changed, 38 insertions, 2 deletions
diff --git a/src/main/java/io/github/moulberry/notenoughupdates/util/ItemResolutionQuery.java b/src/main/java/io/github/moulberry/notenoughupdates/util/ItemResolutionQuery.java index 1ff659ac..48eb6fad 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/util/ItemResolutionQuery.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/util/ItemResolutionQuery.java @@ -194,14 +194,14 @@ public class ItemResolutionQuery { private String resolveEnchantedBookNameFromNBT() { NBTTagCompound enchantments = getExtraAttributes().getCompoundTag("enchantments"); - String enchantName = Iterables.getOnlyElement(enchantments.getKeySet(), null); + String enchantName = IteratorUtils.getOnlyElement(enchantments.getKeySet(), null); if (enchantName == null || enchantName.isEmpty()) return null; return enchantName.toUpperCase(Locale.ROOT) + ";" + enchantments.getInteger(enchantName); } private String resolveRuneName() { NBTTagCompound runes = getExtraAttributes().getCompoundTag("runes"); - String runeName = Iterables.getOnlyElement(runes.getKeySet(), null); + String runeName = IteratorUtils.getOnlyElement(runes.getKeySet(), null); if (runeName == null || runeName.isEmpty()) return null; return runeName.toUpperCase(Locale.ROOT) + "_RUNE;" + runes.getInteger(runeName); } diff --git a/src/main/java/io/github/moulberry/notenoughupdates/util/IteratorUtils.java b/src/main/java/io/github/moulberry/notenoughupdates/util/IteratorUtils.java new file mode 100644 index 00000000..32cd5fad --- /dev/null +++ b/src/main/java/io/github/moulberry/notenoughupdates/util/IteratorUtils.java @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2022 NotEnoughUpdates contributors + * + * This file is part of NotEnoughUpdates. + * + * NotEnoughUpdates is free software: you can redistribute it + * and/or modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation, either + * version 3 of the License, or (at your option) any later version. + * + * NotEnoughUpdates is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with NotEnoughUpdates. If not, see <https://www.gnu.org/licenses/>. + */ + +package io.github.moulberry.notenoughupdates.util; + +import java.util.Iterator; + +public class IteratorUtils { + + public static <T> T getOnlyElement(Iterator<T> it, T defaultValue) { + if (!it.hasNext()) return defaultValue; + T ret = it.next(); + if (it.hasNext()) return defaultValue; + return ret; + } + + public static <T> T getOnlyElement(Iterable<T> it, T defaultValue) { + return getOnlyElement(it.iterator(), defaultValue); + } +} |