Skip to content

Commit

Permalink
Refactor WikiLookup to use optionals
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinthegreat1 committed May 4, 2024
1 parent ed98c8b commit 40d395f
Showing 1 changed file with 11 additions and 23 deletions.
34 changes: 11 additions & 23 deletions src/main/java/de/hysky/skyblocker/skyblock/item/WikiLookup.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

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;
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;
Expand All @@ -20,7 +20,6 @@
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(
Expand All @@ -31,30 +30,19 @@ public static void init() {
));
}

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);
}
}
}
});
}

}

0 comments on commit 40d395f

Please sign in to comment.