diff options
author | Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> | 2024-05-04 11:17:02 -0400 |
---|---|---|
committer | Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> | 2024-05-04 11:17:02 -0400 |
commit | 40d395f1460ce17ccd433644bf989c98554e6949 (patch) | |
tree | d2bc0369e0213c986a4b78b1060d44d6492d0c2a | |
parent | ed98c8b92634f494234cb05c2806ee7b337b6675 (diff) | |
download | Skyblocker-40d395f1460ce17ccd433644bf989c98554e6949.tar.gz Skyblocker-40d395f1460ce17ccd433644bf989c98554e6949.tar.bz2 Skyblocker-40d395f1460ce17ccd433644bf989c98554e6949.zip |
Refactor WikiLookup to use optionals
-rw-r--r-- | src/main/java/de/hysky/skyblocker/skyblock/item/WikiLookup.java | 34 |
1 files changed, 11 insertions, 23 deletions
diff --git a/src/main/java/de/hysky/skyblocker/skyblock/item/WikiLookup.java b/src/main/java/de/hysky/skyblocker/skyblock/item/WikiLookup.java index c954953e..19bef1e1 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/item/WikiLookup.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/item/WikiLookup.java @@ -2,6 +2,7 @@ package de.hysky.skyblocker.skyblock.item; import de.hysky.skyblocker.config.SkyblockerConfigManager; import de.hysky.skyblocker.skyblock.itemlist.ItemRepository; +import de.hysky.skyblocker.utils.Constants; import de.hysky.skyblocker.utils.ItemUtils; import de.hysky.skyblocker.utils.Utils; import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper; @@ -9,7 +10,6 @@ import net.minecraft.client.option.KeyBinding; import net.minecraft.client.util.InputUtil; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.screen.slot.Slot; -import net.minecraft.text.Text; import net.minecraft.util.Util; import org.lwjgl.glfw.GLFW; import org.slf4j.Logger; @@ -20,7 +20,6 @@ import java.util.concurrent.CompletableFuture; public class WikiLookup { private static final Logger LOGGER = LoggerFactory.getLogger(WikiLookup.class); public static KeyBinding wikiLookup; - private static String id; public static void init() { wikiLookup = KeyBindingHelper.registerKeyBinding(new KeyBinding( @@ -31,30 +30,19 @@ public class WikiLookup { )); } - public static void getSkyblockId(Slot slot) { - //Grabbing the skyblock NBT data - //Setting to null prevents a bug where using wiki lookup on an empty slot would bring up the last looked up page - id = ItemUtils.getItemIdOptional(slot.getStack()).orElse(null); - } - public static void openWiki(Slot slot, PlayerEntity player) { - if (Utils.isOnSkyblock() && SkyblockerConfigManager.get().general.wikiLookup.enableWikiLookup) { - getSkyblockId(slot); - - //Fixes a crash where using wiki lookup for the first time on an item that doesn't have an id (because id is initialized to null) - //and now also because we set it to null above for said reason - if (id != null) { - try { - String wikiLink = ItemRepository.getWikiLink(id, player); - if (wikiLink != null) CompletableFuture.runAsync(() -> Util.getOperatingSystem().open(wikiLink)); - } catch (IndexOutOfBoundsException | IllegalStateException e) { - LOGGER.error("[Skyblocker] Error while retrieving wiki article...", e); - if (player != null) { - player.sendMessage(Text.of("[Skyblocker] Error while retrieving wiki article, see logs..."), false); - } + if (!Utils.isOnSkyblock() || !SkyblockerConfigManager.get().general.wikiLookup.enableWikiLookup) return; + + ItemUtils.getItemIdOptional(slot.getStack()).map(id -> ItemRepository.getWikiLink(id, player)).ifPresent(wikiLink -> { + try { + CompletableFuture.runAsync(() -> Util.getOperatingSystem().open(wikiLink)); + } catch (IndexOutOfBoundsException | IllegalStateException e) { + LOGGER.error("[Skyblocker] Error while retrieving wiki article...", e); + if (player != null) { + player.sendMessage(Constants.PREFIX.get().append("Error while retrieving wiki article, see logs..."), false); } } - } + }); } } |