From d203640d5296bde151596388aa6df332393d0a1c Mon Sep 17 00:00:00 2001 From: Roman / Linnea Gräf Date: Fri, 9 Sep 2022 15:52:20 +0200 Subject: Fix item resolution query crashing on some enchant books (#262) --- .../notenoughupdates/util/ItemResolutionQuery.java | 4 +-- .../notenoughupdates/util/IteratorUtils.java | 36 ++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 src/main/java/io/github/moulberry/notenoughupdates/util/IteratorUtils.java (limited to 'src') 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 . + */ + +package io.github.moulberry.notenoughupdates.util; + +import java.util.Iterator; + +public class IteratorUtils { + + public static T getOnlyElement(Iterator it, T defaultValue) { + if (!it.hasNext()) return defaultValue; + T ret = it.next(); + if (it.hasNext()) return defaultValue; + return ret; + } + + public static T getOnlyElement(Iterable it, T defaultValue) { + return getOnlyElement(it.iterator(), defaultValue); + } +} -- cgit